Android: decodeFile always returns null for file in internal storage

后端 未结 7 1587
长情又很酷
长情又很酷 2020-12-09 16:59

I have a file saved locally into the application\'s private storage. I have verified it exists, however whenever I call BitmapFactory.decodeFile it always retur

7条回答
  •  难免孤独
    2020-12-09 17:16

    For me I was getting image from locally saved URL something like "file:///storage/emulated/0/...." (I have used Phonegap plugin to capture image. Plugin was giving me image path, which I need to use in native code)

    Here is the code snippet which worked for me.

    String captured_image_info = "file:///storage/emulated/0/Android/data/com.testapp/cache/1493809796526.jpg"
    Uri uri=Uri.parse(captured_image_info);
    largeLog("uri", "" + uri);
    
    InputStream imageStream = getContentResolver().openInputStream(uri);
    
    Bitmap bm = BitmapFactory.decodeStream(imageStream);
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    
    byte[] decodedBytes = baos.toByteArray();
    
    Bitmap img_captured_image = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
    

提交回复
热议问题