简体   繁体   English

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

[英]Show images in specific folder and open selected image

I want to create an activity When I open the activity it to show images in specific folder called "myfolder" and when I select image I need it to show it in an imageview. 我想创建一个活动当我打开活动以在名为“ myfolder”的特定文件夹中显示图像时,当我选择图像时,我需要它以在imageview中显示它。

The code I created is working but it is showing me all images in my device and when I select image it return to the app but doesn't show the image in the imageview 我创建的代码可以正常工作,但是会向我显示设备中的所有图像,当我选择图像时,它会返回应用程序,但不会在imageview中显示图像

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;

Try to convert URI to Path 尝试将URI转换为Path

   public static String getPathFromURI(Context context, Uri uri) {
      String[] proj = { MediaStore.Images.Media.DATA };
      String result = null;

      CursorLoader cursorLoader = new CursorLoader(
              context, 
        uri, proj, null, null, null);        
      Cursor cursor = cursorLoader.loadInBackground();

      if(cursor != null){
       int column_index = 
         cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       result = cursor.getString(column_index);
      }
      return result;  
}

and set path inside glide 并设置滑行内的路径

may be work 可能是工作

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

相关问题 在android中显示图像sd卡特定的文件夹 - show images sd card specific folder in android 在 Android Studio 的 Gallery 中打开特定文件夹(不是图像) - Open a specific folder (not a image) in Gallery in Android Studio 从“最近的图像”文件夹中选择图像时,图片路径为空 - Picture path is null when selected an Image from the "Recent Images" folder 从Imagebutton打开特定的图像和文件夹,但仍然可以滑动到目录中的其他图像和文件夹吗? - Open specific image and folder from Imagebutton, but still swipe to others in the directory? 打开Android默认库中特定文件夹中的所有图像 - Open all image from specific folder in Android default gallery cordova-plugin-camera:如何在特定文件夹中打开图像而不是显示图库中的所有图像 - cordova-plugin-camera : how to open images in specific folder instead of showing all images from gallery 如何显示特定文件夹中的最后拍摄图像? - How to show last taken image from specific folder? 我想从相机文件夹图像显示中将图像添加到pdf中,可以在pdf中添加图像吗? - i want to add images to pdf from camera folder image show can i add a images in pdf 从在线文件夹中打开图像 - Open images from online folder 选择特定选项卡上的打开活动 - open activity on specific tab selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM