2015-04-22 35 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); 
} 
} 
相关问题