简体   繁体   English

Android从内部/外部存储选择图像

[英]Android select image from internal / external storage

I am trying to add images from both the internal and external memory of a device to my application. 我正在尝试将设备的内部和外部存储器中的图像添加到我的应用程序中。 I am able to open the gallery intent and get the path of the file but then i am not able to convert it to a bitmap for my ImageView. 我能够打开图库意图并获取文件的路径,但是随后我无法将其转换为ImageView的位图。 Here is thecode for the onClick listener for the icon that calls the gallery: 以下是用于调用图库的图标的onClick侦听器的代码:

icoGallery = (ImageView) findViewById(R.id.icoGallery);
icoGallery.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        galleryIntent.setType("image/*");
        startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
    }
});

Here is the code for the onActivitResult: 这是onActivitResult的代码:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null !=data){
        Uri selectedImageUri = data.getData();
        String[] projection = {MediaStore.Images.Media.DATA};
        @SuppressWarnings("deprecation")
        Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
        cursor.moveToFirst();

        int column_index = cursor.getColumnIndex(projection[0]);
        imagePath = cursor.getString(column_index);
        cursor.close();

        imageFile = new File(imagePath);
        if (imageFile.exists()){
            Bitmap imageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            imgPhoto.setImageBitmap(imageBitmap);
        }

    } else {
        Toast.makeText(context, "You have not selected and image", Toast.LENGTH_SHORT).show();
    }
}

I have included the following permission to the manifest: 我已对清单包含以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

I keep getting the following error 我不断收到以下错误

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/20170215_152240.jpg: open failed: EACCES (Permission denied)

I believe that part of the reason why it is failing is because the device only has internal storage. 我认为失败的部分原因是该设备仅具有内部存储。 Is there a way to add an image from the device that is either on the local storage or the external one? 有没有一种方法可以从本地存储或外部存储中的设备添加图像? Or do I have to make a function that asks the user if they want to use the internal or external storage? 还是我必须创建一个功能询问用户是否要使用内部或外部存储?

Thanks in advance :) 提前致谢 :)

EDIT: The error was caused by not initializing the ImageView. 编辑:错误是由于未初始化ImageView引起的。 However, after coming back from the Gallery activity and returning the path of the file, the image is not displayed on the ImageView. 但是,从Gallery活动返回并返回文件的路径后,该图像不会显示在ImageView上。 It changes to the background color. 变为背景色。

You can set Uri directly to ImageView Like this: 您可以将Uri直接设置为ImageView如下所示:

Uri selectedImageUri = data.getData();
imageView.setImageURI(selectedImageUri);

And then get Bitmap from ImageView: 然后从ImageView获取位图:

 Drawable drawable =  imageView.getDrawable();
 Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

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

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