2015-04-22 34 views
0

如何設置使用getData在imageview上捕獲的圖像?當我運行它時,點擊確定應用程序關閉。如何在imageview上設置捕獲的圖像

if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 



       Bitmap photo = (Bitmap) data.getExtras().get("data"); 
       first_image.setImageBitmap(photo); 


       String[] projection = {}; 
       Cursor cursor = getContentResolver().query(fileUri, projection, null, null, null); 
       int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToFirst(); 
       String capturedImageFilePath = cursor.getString(column_index_data); 

回答

2

你可以試試這個

追加這個代碼的獲取路徑之後

File imgFile = new File(capturedImageFilePath); 

if(imgFile.exists()){ 

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 

    myImage.setImageBitmap(myBitmap); 

} 
0

你需要從數據的圖像路徑,你的代碼應該是這樣的:

Uri uri = data.getData(); 
String path = convertMediaUriToPath(getApplicationContext(), uri); 
Bitmap b = BitmapFactory.decodeFile(path); 
img.setImageBitmap(b); 

其中

public String convertMediaUriToPath(Context context, Uri uri) { 
    Cursor cursor = null; 
    try { 
     String[] proj = {MediaStore.MediaColumns.DATA}; 
     cursor = context.getApplicationContext() 
       .getContentResolver().query(uri, proj, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
     cursor.moveToFirst(); 
     String path = cursor.getString(column_index); 
     Log.e("path", path); 
     return path; 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
} 

希望這有助於:)

1

使用此:

if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
Uri selectedImage = data.getData(); 
     Log.d("selectedimage", ""+selectedImage); 
     String[] filePath = { MediaStore.Images.Media.DATA }; 
     Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); 
     c.moveToFirst(); 
     int columnIndex = c.getColumnIndex(filePath[0]); 
     String picturePath = c.getString(columnIndex); 
     c.close(); 
     Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); 

     Log.w("path of image from gallery......******************.........", picturePath+""); 
     ImageView m1= (ImageView) findViewById(R.id.imageView1); 
} 
} 
相關問題