2012-10-10 87 views
0

我想從/mnt/sdcard/img.jpg中將圖像讀入ImageView。Android:位圖路徑不正確

File image = new File(path); 
      Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath()); 
      webView1.setImageBitmap(bm); 

我從圖庫中獲取路徑,我的應用說路徑不存在。但在文件管理器中它是存在的。

我該如何解決?

if (arg0 == button4){ 
      Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT); 
      photoPickerIntent.setType("image/*"); 
      startActivityForResult(photoPickerIntent, 1); 

     } 

如何到映像文件的路徑:

private String UriToBit(Uri uri) { 
     String[] proj = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = managedQuery(uri, proj, null, null, null); 
     int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(index); 
    } 
+0

你能過去錯誤描述嗎? –

回答

0

嗨下面的代碼,我正在用於顯示的SD卡攝取的圖像到ImageView的。

selectimage = (Button) findViewById(R.id.selectimg); 

    selectimage.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(i, PICK_FROM_FILE);     

      } 
     } 
    }); 

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

      if (resultCode == RESULT_OK) { 


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

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

     int columnIndex = cursor.getColumnIndexfilePathColumn[0]); 
     String filePath = cursor.getString(columnIndex); 
     cursor.close(); 

     yourSelectedImage = BitmapFactory.decodeFile(filePath); 
     img.setImageBitmap(yourSelectedImage);