简体   繁体   中英

Get bitmap from uri Android API < 19

I'm coding an custom camera picker for an app.

This is how i manage camera intent :

@SuppressWarnings("ResultOfMethodCallIgnored")
@NonNull
private Intent getCameraIntent()
{
     mCurrentImageFileName = "image" + System.currentTimeMillis() + ".jpg"; // create a default jpg image
     File imagesDir = new File( mActivity.getFilesDir(), "images"); // init a images directory creation
     imagesDir.mkdirs(); // create images directory
     File vFile = new File( imagesDir, mCurrentImageFileName );
     try { vFile.createNewFile();  } catch (IOException e) { e.printStackTrace(); }

     Intent vCameraIntent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE ); // init the default camera intent
     String vAuthority = mActivity.getPackageName() + ".******"; // get path authority ( used to filter a specific folder )

    final Uri outputUri;

    if( Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ) { outputUri = Uri.fromFile( vFile ); } // fix kitkat issue
    else { outputUri = FileProvider.getUriForFile( mActivity.getApplicationContext(), vAuthority, vFile ); }

    vCameraIntent.putExtra( MediaStore.EXTRA_OUTPUT, outputUri );

     LogUtils.setDebugLog( "debugOutputUri %s", "output uri is : " + outputUri );

     mActivity.grantUriPermission(
             "com.google.android.GoogleCamera",
             outputUri,
             Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
     );
     return vCameraIntent;
 }

And how i handle the result to get bitmap :

Bitmap vBitmap = MediaStore.Images.Media.getBitmap( mActivity.getContentResolver(), vImageUri ); // get bitmap

vImageUri is :

  • In API > 19 : file:///data/user/0/*****/files/images/image1548767548901.jpg
  • In API < 19 : file:///data/data/*****/files/images/image1548767438290.jpg

In API > 19 getBitmap is working perfectly but in API < 19 getBitmap returns always null .

My App have all the permissions required


Can anyone help me please ?

Here is the official solution from Android Developers documentation .

private Bitmap getBitmapFromUri(Uri uri) throws IOException {
        ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        parcelFileDescriptor.close();
        return image;
}

NOTE:

You should complete this operation on a background thread, not the UI thread.

Try Glide, it is compatible above API level 14

Add dependencies

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

then add your code

Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }
         });

The other location is an internal storage.Can you please try below

ContextWrapper cw = new ContextWrapper(context);
//path to /data/data/yourapp/app_data/dirName
File directory = cw.getDir("dirName", Context.MODE_PRIVATE);          
File mypath=new File(directory,"imagename.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(mypath));

I found a solution for this issue .For getting Uri you must use two different manners: 1- In operation page for api 19 and less you must use Uri.fromFile() and 2- for api > 19 please use File Provider .If you use this instruction , always the method of

MediaStore.Images.Media.getBitmap() works fine and returns your bitmap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM