在特定文件夹中显示图像并打开所选图像

时间:2017-11-24 12:07:43

标签: android image android-intent imageview directory

我想创建一个活动当我打开活动时,它会在特定文件夹中显示图像,名为" myfolder"当我选择图像时,我需要它在图像视图中显示它。

我创建的代码正在运行,但它会向我显示设备中的所有图像,当我选择图像时,它会返回应用但不会在图像视图中显示图像

ImageView imageview1;

private static final int REQUEST_OPEN_RESULT_CODE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);

    imageview1 = (ImageView) findViewById(R.id.imageview);
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/myfolder/");
    intent.setDataAndType(uri, "image/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_OPEN_RESULT_CODE);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData){
    if (requestCode == REQUEST_OPEN_RESULT_CODE && requestCode == RESULT_OK){
        Uri uri = null;
        if(resultData != null){
            uri = resultData.getData();

            Glide.with(this)
                    .load(uri)
                    .into(imageview1);
        }
    }
}
private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return bitmap;
}
private GestureDetector gs = null;

0 个答案:

没有答案