【问题标题】:how to show image in image view如何在图像视图中显示图像
【发布时间】:2016-03-04 09:36:43
【问题描述】:

当我点击图库视图时,我正在开发 android 自定义相机应用程序,它显示移动默认图库并显示我点击的照片,但是当我点击图库然后点击图片时,我想要在我的自定义视图中显示如何实现

  public void clickedGallery(View view) {
    if (MyDebug.LOG)
        Log.d(TAG, "clickedGallery");
    //Intent intent = new Intent(Intent.ACTION_VIEW, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    Uri uri = null;
    Media media = getLatestMedia();
    if (media != null) {
        uri = media.uri;
    }

    if (uri != null) {
        // check uri exists
        if (MyDebug.LOG)
            Log.d(TAG, "found most recent uri: " + uri);
        try {
            ContentResolver cr = getContentResolver();
            ParcelFileDescriptor pfd = cr.openFileDescriptor(uri, "r");
            if (pfd == null) {
                if (MyDebug.LOG)
                    Log.d(TAG, "uri no longer exists (1): " + uri);
                uri = null;
            }
            pfd.close();
        } catch (IOException e) {
            if (MyDebug.LOG)
                Log.d(TAG, "uri no longer exists (2): " + uri);
            uri = null;
        }
    }
    if (uri == null) {
        uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }
    if (!is_test) {
        // don't do if testing, as unclear how to exit activity to finish test (for testGallery())
        if (MyDebug.LOG)
            Log.d(TAG, "launch uri:" + uri);
        final String REVIEW_ACTION = "com.android.camera.action.REVIEW";
        try {
            // REVIEW_ACTION means we can view video files without autoplaying
            Intent intent = new Intent(REVIEW_ACTION, uri);
            this.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            if (MyDebug.LOG)
                Log.d(TAG, "REVIEW_ACTION intent didn't work, try ACTION_VIEW");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            // from http://stackoverflow.com/questions/11073832/no-activity-found-to-handle-intent - needed to fix crash if no gallery app installed
            //Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("blah")); // test
            if (intent.resolveActivity(getPackageManager()) != null) {
                this.startActivity(intent);
            } else {
                preview.showToast(null, R.string.no_gallery_app);
            }
        }
    }
}

【问题讨论】:

    标签: android android-intent android-camera android-gallery


    【解决方案1】:

    这里是完整的代码,您可以将requestCode 修改为您在代码中声明的任何内容,您可以在下一个活动中传递 Uri 或文件的实际位置。

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode != RESULT_CANCELED) {
                if (requestCode == SELECT_FILE && (data != null)) {
                    Uri selectedImageUri = data.getData();
                    String[] projection = {MediaStore.MediaColumns.DATA};
                    Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null,
                            null);
                    assert cursor != null;
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                    cursor.moveToFirst();
    
                    String selectedImagePath = cursor.getString(column_index);
                    if(selectedImagePath == null) {
                        selectedImagePath = getActualPathFromUri(selectedImageUri);
                    }
                    cursor.close();
                } else {
                    super.onActivityResult(requestCode, resultCode, data);
                }
            }
        }
    
        private String getActualPathFromUri(Uri selectedImageUri) {
            Bitmap bitmap = null;
            try {
                bitmap = getBitmapFromUri(selectedImageUri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(bitmap == null) {
                return null;
            }
    
            File imageFileFolder = new File(getCacheDir(),"appName");
            if( !imageFileFolder.exists() ){
                imageFileFolder.mkdir();
            }
    
            FileOutputStream out = null;
    
            File imageFileName = new File(imageFileFolder, "appName-" + System.currentTimeMillis() + ".jpg");
            try {
                out = new FileOutputStream(imageFileName);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();
            } catch (IOException e) {
                Log.i("Exception", e.getMessage());
            } finally {
                if(out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return imageFileName.getAbsolutePath();
        }
    
        private Bitmap getBitmapFromUri(Uri uri) throws IOException {
            ParcelFileDescriptor parcelFileDescriptor =
                    getContentResolver().openFileDescriptor(uri, "r");
            assert parcelFileDescriptor != null;
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
            parcelFileDescriptor.close();
            return image;
        }
    

    【讨论】:

    • 你不是在使用意图打开画廊或相机吗?
    • 请看我的代码,只有在点击画廊时才会显示手机的默认照片查看器
    【解决方案2】:

    当您在拍照后从相机返回或从图库返回时,您的onActivityResult 方法将调用。所以像下面这样覆盖你的 onActivityResult

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
             if (resultCode == RESULT_OK) {
       //Read your image here and set in your imageView 
    }
        }
    

    编辑:

    将图像设置为图像视图

    onActivityResult 内部

    if(data.getData()==null){
        bitmap = (Bitmap)data.getExtras().get("data");
    }else{
        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
    }
    
    yourImageView.setImageBitmap(img);
    

    【讨论】:

    • 如何在此代码中设置图像我想在自定义视图中显示图像我制作类图像视图如何在此活动中设置图像
    • 谢谢,但是当我点击图库按钮并打开新活动并在那里显示图像时,我想打开图像
    • @HarpalSingh 当您在onActivityResult 中获取位图时,将其保存在缓存目录中的文件中,并通过意图将文件名传递给您要将图像设置到图像视图中的新活动。
    • 没问题,只需保存图像路径并从下一个活动中显示它
    • 正是我的意思@AmitTiwari
    猜你喜欢
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多