Search code examples
javaandroidfilenotfoundexceptionbitmapfactory

Unable to decode stream java.io.FileNotFoundException in android?


I am new to android and I am getting this error when I try to use decodefile().

Here is the code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if ((requestCode == PICK_FROM_GALLERY ) && (resultCode == RESULT_OK) &&       (data != null)){
         selectedImageUri = data.getData( );
         Log.e("Path1", ""+selectedImageUri);
         path1 = selectedImageUri.getPath();
         file = new File(path1);
         path =file.getAbsolutePath();
         Log.e("Path2", file.getAbsolutePath());

         mImageView.setImageURI(selectedImageUri);
    }}

protected void badButtonPressed() {
    final long startTime = SystemClock.uptimeMillis();


    BitmapFactory.Options options = new BitmapFactory.Options();
    // Part 1: Decode image
   >> Bitmap unscaledBitmap = BitmapFactory.decodeFile(path, options);
     imageHeight = options.outHeight;
     imageWidth = options.outWidth;
    Log.e("w", ""+imageWidth);
    Log.e("h", ""+imageHeight);

Please tell me my mistake.


Solution

  • found this answer

    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        intent.setType("image/*");
                        startActivityForResult(intent, SELECT_IMAGE);
    
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_IMAGE) {
                    try {
                        selectedImagePath = getAbsolutePath(intent.getData());
                        mImageCaptureUri = intent.getData();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    
        public String getAbsolutePath(Uri uri) {
            String[] projection = {MediaStore.MediaColumns.DATA};
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index =    cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }