Home > Back-end >  Is it possible to convert a uri arraylist to bitmap
Is it possible to convert a uri arraylist to bitmap

Time:11-18

I would like to convert my Uri ArrayList into bitmap so that I can do some image processing on it and have it return a Bitmap ArrayList. Here is my current code. Any help or pointers are appreciated! (I'm also relatively new to java and android studio so sorry if the codes are a mess)

ArrayList<Uri> uris = new ArrayList<Uri>();
        for (int i = 0; i < f.size(); i  ) {
            Uri uri = Uri.parse(f.get(i));
            uris.add(uri);
        }

        if (uris != null) {
            Log.d("PreviewImageAdapter", "uri: "   uris);
            //Bitmap bmp = null;
            try {
                int position = 0;
                //Bitmap bmp = BitmapFactory.decodeFile(paths.get(position));
                Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), uris.get(position));

                Mat selected_image = new Mat(bmp.getHeight(), bmp.getWidth(), CvType.CV_8UC4);
                Utils.bitmapToMat(bmp, selected_image);
                selected_image = objectDetectorClass.recognizePhoto(selected_image);
                Bitmap bitmap1 = null;
                bitmap1 = Bitmap.createBitmap(selected_image.cols(), selected_image.rows(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(selected_image, bitmap1);

                ArrayList<Bitmap> bitmap = new ArrayList<Bitmap>();
                bitmap.add(bitmap1);

            } catch (IOException e) {
                e.printStackTrace();
            }

Here is the logcat error I'm getting

2021-11-15 10:07:29.500 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor D/PreviewImageAdapter: uri: [/storage/emulated/0/Pictures/VideoEditor15/extract_picture007.jpg, /storage/emulated/0/Pictures/VideoEditor15/extract_picture006.jpg, /storage/emulated/0/Pictures/VideoEditor15/extract_picture003.jpg, /storage/emulated/0/Pictures/VideoEditor15/extract_picture002.jpg, /storage/emulated/0/Pictures/VideoEditor15/extract_picture004.jpg, /storage/emulated/0/Pictures/VideoEditor15/extract_picture001.jpg, /storage/emulated/0/Pictures/VideoEditor15/extract_picture005.jpg, /storage/emulated/0/Pictures/VideoEditor15/extract_picture008.jpg]
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err: java.io.FileNotFoundException: No content provider: /storage/emulated/0/Pictures/VideoEditor15/extract_picture007.jpg
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1979)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1808)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.content.ContentResolver.openInputStream(ContentResolver.java:1485)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:2094)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at videoeditor.bhuvnesh.com.ffmpegvideoeditor.activity.PreviewImageActivity.onCreate(PreviewImageActivity.java:91)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.Activity.performCreate(Activity.java:8207)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.Activity.performCreate(Activity.java:8191)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3808)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.os.Looper.loop(Looper.java:246)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:8633)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
2021-11-15 10:07:29.501 16672-16672/videoeditor.bhuvnesh.com.ffmpegvideoeditor W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

CodePudding user response:

Yes, it is possible to convert Uri to bitmap, but this error not looks like a bitmap conversion error.

try this, add this to your res/xml folder. //external_files

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!--<files-path name="my_images" path="images/"/>
    <files-path name="my_docs" path="docs/"/>-->
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
    <external-cache-path
        name="cache"
        path="camera" />
</paths>

after that, add this in application tag in manifest.

  <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/external_files" />
    </provider>
  • Related