2015-10-17 50 views
-1

我是新来的android和我遇到这个错误,当我尝试使用decodefile()无法解码Android中的流java.io.FileNotFoundException?

下面是代码

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); 

请告诉我,我的错误。

+1

请张贴完整堆栈跟踪。 –

+0

欢迎来到Stack Overflow。我编辑了你的帖子来解决一些英文问题。我还用两个星号加粗了函数名称。 我看不到你提到的错误。请修改您的帖子并向我们显示错误(使用>在该行的开头标记它)。 –

+1

@RohutGupta你为什么加粗了函数名?当代码格式化已经存在? – EJP

回答

0

发现这个答案

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; 
    } 
-1

请尝试。

int PICK_FROM_GALLERY = 3; 
String path; 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == PICK_FROM_GALLERY 
      && resultCode == Activity.RESULT_OK) { 

     try { 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      path = cursor.getString(columnIndex); 



      cursor.close(); 
     } catch (NullPointerException e) { 

     } 

    } 
} 

public void pickFromGallery() 
{ 
    Intent i = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(i, PICK_FROM_GALLERY); 
} 
+0

仍为空指针错误在“路径”中没有什么是没有的 –

+0

当你调用badButtonPressed()? – Hasanaga

+0

我能够从图库中显示图像,但是当我试图在那个图像上执行缩放时,它给了我这个错误 –

相关问题