android – 如何从Gallery中获取图像URI

android – 如何从Gallery中获取图像URI,第1张

概述我需要从图库中获取图像.我可以打开图库来选择图像,但在选择图像后它不会返回任何内容.我需要将该fileUri发送到另一个活动并在 ImageView上显示它. 我可以做这个相机,就像按钮点击它调用相机然后我捕获图像并将其发送到另一个活动. 但我不明白我用于画廊的内容.有人请帮帮我. 更新 This is I’m using to get image from Camera private void 我需要从图库中获取图像.我可以打开图库来选择图像,但在选择图像后它不会返回任何内容.我需要将该fileUri发送到另一个活动并在 ImageVIEw上显示它.
我可以做这个相机,就像按钮点击它调用相机然后我捕获图像并将其发送到另一个活动.

但我不明白我用于画廊的内容.有人请帮帮我.

更新

This is I’m using to get image from Camera

private voID captureImage() {    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    fileUri = getoutputMediafileUri(MEDIA_TYPE_IMAGE);    intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);    // start the image capture Intent    startActivityForResult(intent,CAMERA_CAPTURE_IMAGE_REQUEST_CODE);}

This is m using to fetch image from gallery
But I want to do it in a same way as I’m doing for captureImage(),so that i can send ImageUri to other activity

private voID browseImage(){    try {    Intent galleryIntent = new Intent(Intent.ACTION_PICK,androID.provIDer.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);        // Start the Intent         fileUri = getoutputMediafileUri(PICK_IMAGE);//this is m using for camera          Log.w("ImageAddressOnClick pr",""+fileUri);        startActivityForResult(galleryIntent,galLERY_IMAGE_PICK);    } catch (Exception e) {        Toast.makeText(getApplicationContext(),e.getMessage()+"ye show hora h",Toast.LENGTH_LONG).show();        Log.e(e.getClass().getname(),e.getMessage(),e);    }

OnActivityResult Method

@OverrIDeprotected voID onActivityResult(int requestCode,int resultCode,Intent data) {    // if the result is capturing Image     super.onActivityResult(requestCode,resultCode,data);    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {        if (resultCode == RESulT_OK) {            // successfully captured the image            // launching upload activity            launchUploadActivity(true);        } else if (resultCode == RESulT_CANCELED) {            // user cancelled Image capture            Toast.makeText(getApplicationContext(),"User cancelled image capture",Toast.LENGTH_SHORT)                    .show();        } else {            // Failed to capture image            Toast.makeText(getApplicationContext(),"Sorry! Failed to capture image",Toast.LENGTH_SHORT)                    .show();        }    }    else if(requestCode == galLERY_IMAGE_PICK && resultCode == RESulT_OK            && null != data)            {       Uri selectedImage = data.getData();         String[] filePathColumn = { MediaStore.Images.Media.DATA };         Log.w("onActivityResult","chali ye onActivityResult "+selectedImage);         // Get the cursor         Cursor cursor = getContentResolver().query(selectedImage,filePathColumn,null,null);         // Move to first row         cursor.movetoFirst();         //int columnIndex = cursor.getColumnIndex(filePathColumn[0]);       //  imgDecodableString = cursor.getString(columnIndex);         cursor.close();         launchUploadActivity(true);            }          }private voID launchUploadActivity(boolean isImage){    Intent i = new Intent(MainActivity.this,UploadActivity.class);    i.putExtra("filePath",fileUri.getPath());    i.putExtra("isImage",isImage);    startActivity(i);}/** * ------------ Helper Methods ----------------------  * *//** * Creating file uri to store image/vIDeo */public Uri getoutputMediafileUri(int type) {    return Uri.fromfile(getoutputMediafile(type));}/** * returning image / vIDeo */private static file getoutputMediafile(int type) {    // External sdcard location    file mediaStorageDir = new file(            Environment                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),Config.IMAGE_DIRECTORY_name);    // Create the storage directory if it does not exist    if (!mediaStorageDir.exists()) {        if (!mediaStorageDir.mkdirs()) {            Log.d(TAG,"Oops! Failed create "                    + Config.IMAGE_DIRECTORY_name + " directory");            return null;        }    }    // Create a media file name    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.getDefault()).format(new Date());    file mediafile;    if (type == MEDIA_TYPE_IMAGE) {        mediafile = new file(mediaStorageDir.getPath() + file.separator                + "img_" + timeStamp + ".jpg");    } else {        return null;    }    return mediafile;}

I’m sending data through launchUploadActivity();

提前致谢 :)

解决方法 onActivityResult的变化:

else if(requestCode == galLERY_IMAGE_PICK && resultCode == RESulT_OK        && null != data)        {            Uri selectedImage = data.getData();                     String picturePath = getRealPathFromURI(selectedImage,this);           Intent i = new Intent(MainActivity.this,UploadActivity.class);           i.putExtra("filePath",selectedImage);           i.putExtra("isImage",isImage);           startActivity(i);        }      }}public String getRealPathFromURI(Uri contentURI,Activity context) {        String[] projection = { MediaStore.Images.Media.DATA };        @SuppressWarnings("deprecation")        Cursor cursor = context.managedquery(contentURI,projection,null);        if (cursor == null)            return null;        int column_index = cursor                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);        if (cursor.movetoFirst()) {            String s = cursor.getString(column_index);            // cursor.close();            return s;        }        // cursor.close();        return null;    }
总结

以上是内存溢出为你收集整理的android – 如何从Gallery中获取图像URI全部内容,希望文章能够帮你解决android – 如何从Gallery中获取图像URI所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127913.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存