繁体   English   中英

如何在imageview上设置捕获的图像

[英]how to set captured image on imageview

如何使用getData设置在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);

你可以试试这个

在获取路径后追加此代码

File imgFile = new  File(capturedImageFilePath );

if(imgFile.exists()){

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

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

    myImage.setImageBitmap(myBitmap);

}

用这个:

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

您需要从数据获取图像路径,您的代码应如下所示:

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

希望这可以帮助 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM