Skip to content Skip to sidebar Skip to footer

Android - Startactivityforresult For Getting File's Path

I'm writing a simple image viewer for Android. My app have the 'Open file from Gallery' button. So, when I've choosed the image from Gallery, it must return the path to file into m

Solution 1:

Use this code

protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {  

   super.onActivityResult(requestCode, resultCode, data); 
   if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){

      UriselectedImage= data.getData();
      String[] filePathColumn = { MediaStore.Images.Media.DATA };
      Cursorcursor= getContentResolver().query(selectedImage,filePathColumn, null, null, null);
      cursor.moveToFirst();
      intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
      StringpicturePath= cursor.getString(columnIndex);
      cursor.close();
}

Solution 2:

You can also get image using URI :

try this

try {
            UriselectedImage= selectedImageUri;
            //getContentResolver().notifyChange(selectedImage, null);ContentResolvercr= getContentResolver();
            Bitmap bitmap;
            bitmap = android.provider.MediaStore.Images.Media
                    .getBitmap(cr, selectedImage);
            rptImage.setImageBitmap(bitmap);

        } catch (Exception e) {
            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                    .show();
            Log.e("Camera", e.toString());
        }

OR simply this :

Try with:

ImageView.setImageUri(Uri.fromFile(newFile("/sdcard/cats.jpg")));

Or with:

ImageView.setImageUri(Uri.parse(newFile("/sdcard/cats.jpg").toString()));

Solution 3:

try below code

protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_FILE && resultCode == Activity.RESULT_OK) {

   UrimImageCaptureUri= data.getData(); 
   String [] proj       = {MediaStore.Images.Media.DATA};
   Cursorcursor= managedQuery( contentUri, proj, null, null,null);
   if (cursor != null){
        intcolumn_index= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        Stringpath= cursor.getString(column_index);
   }
}


}

here variable path will return path to your selected image.

Post a Comment for "Android - Startactivityforresult For Getting File's Path"