简体   繁体   中英

Getting a uri from a bitmap

I'm trying to get a uri from a bitmap so that I can upload it to Firebase's storage, but my path keeps coming back as null. After some googling, I tried adding <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> to my manifest to fix it, but it didn't have any effect. Whenever I debug my program in android studio, it seems to break when creating path .

Here's the code:

private Uri getImageUri(Context inContext, Bitmap myImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    myImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), myImage, "Title", null);
    return Uri.parse(path);
}

This an example of how I am using the method

Uri file = getImageUri(getApplicationContext(), uploadPhoto);
StorageReference picRef = storageRef.child("images/" + file.getLastPathSegment());
// Uploads the file
UploadTask uploadTask = picRef.putFile(file);

If you already have the image in memory, why do you even need a reference to its path on disk? Just directly upload the image

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