2016-12-29 136 views
0

爲什麼包裹文件描述符拋出FileNotFounException即使imageUri不爲空或空。我使用Intent從我的畫廊中選擇了一個文件。我還敬酒「imageUri」的價值,它打印。ParcelFileDescriptor投擲文件未發現異常

 ParcelFileDescriptor percelFileDescriptor; 
     Toast.makeText(getApplicationContext(),""+imageUri.toString(),Toast.LENGTH_LONG).show(); 
     try { 
      percelFileDescriptor = getContentResolver().openFileDescriptor(imageUri, "r"); 
      image = BitmapFactory.decodeFileDescriptor(percelFileDescriptor.getFileDescriptor()); 
      modified_img_bitmap = image; 
      percelFileDescriptor.close(); 
      picked_image.setImageBitmap(image); 
      } catch (FileNotFoundException e) { 
      System.out.println("File Not Found: " + e.toString()); 
      Toast.makeText(getApplicationContext(),"File not found: "+e.toString(),Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
      Toast.makeText(getApplicationContext(),"IO Error: "+e.toString(),Toast.LENGTH_LONG).show(); 
      System.out.println("IO Exception: " + e.toString()); 
      } 
     } 

回答

0

我試過你的代碼,那是有效的。請發佈完整的代碼。

這一工程:

private final int GALLERY_REQUEST = 0; 
Uri imageUri; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent pictureActionIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

    startActivityForResult(pictureActionIntent, GALLERY_REQUEST); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == Activity.RESULT_OK) 
    { 
     switch (requestCode) 
     { 
      case GALLERY_REQUEST: 

       if (data != null) 
       { 
        imageUri = data.getData(); 

        Bitmap image = null; 
        Bitmap modified_img_bitmap = null; 
        ImageView picked_image = (ImageView)findViewById(R.id.imageView); 
        ParcelFileDescriptor percelFileDescriptor; 
        Toast.makeText(getApplicationContext(),""+imageUri.toString(),Toast.LENGTH_LONG).show(); 

        try 
        { 
         percelFileDescriptor = getContentResolver().openFileDescriptor(imageUri, "r"); 
         image = BitmapFactory.decodeFileDescriptor(percelFileDescriptor.getFileDescriptor()); 
         modified_img_bitmap = image; 
         percelFileDescriptor.close(); 
         picked_image.setImageBitmap(image); 
        } catch (FileNotFoundException e) 
        { 
         System.out.println("File Not Found: " + e.toString()); 
         Toast.makeText(getApplicationContext(),"File not found: "+e.toString(),Toast.LENGTH_LONG).show(); 
        } catch (IOException e) 
        { 
         Toast.makeText(getApplicationContext(),"IO Error: "+e.toString(),Toast.LENGTH_LONG).show(); 
         System.out.println("IO Exception: " + e.toString()); 
        } 
       } 

       break; 

      default: 
       break; 
     } 
    } 
}