Reputation: 37632
I use following code to open default file manager and find images but when I click on image the Activity closes.
I can select file on the top of the Activity I see the word Open
but when I tap on it it just closes itself.
I would like to catch this event and open selected image in full screen mode in my custom Activity.
int PICKFILE_REQUEST_CODE=10;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
Upvotes: 1
Views: 2224
Reputation: 37632
Finally I got this working.
1. Start File Manager to select file (In my case it is image file)
int PICKFILE_REQUEST_CODE=33; // class property
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT); // It helps to get Image Uri
intent.setType("image/*"); // Filter only images to open
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
2. Use onActivityResult event to get result of selection of the intent with PICKFILE_REQUEST_CODE
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICKFILE_REQUEST_CODE ) {
if(resultCode == Activity.RESULT_OK){
Uri imageUri = data.getData();
Intent intent = new Intent(this, ImageViewer.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent); // Start new intent to see the full sized image ImageViewer
}
}
3. And withing ImageViewer we can do all staff with Image
public class ImageViewer extends AppCompatActivity {
private ImageView imgView;
private String fullScreenInd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_viewer);
Uri myUri = Uri.parse(getIntent().getExtras().getString("imageUri"));
imgView = (ImageView)findViewById(R.id.fullImageView);
imgView.setImageURI(myUri);
imgView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
imgView.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
imgView.setAdjustViewBounds(false);
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
Upvotes: 0
Reputation: 6107
Try using this code
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileuri, "image/*");
startActivity(intent);
Edit:
First use the method to Open gallery for choosing a image file
private void dispatchImageGalleryIntent() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
try {
startActivityForResult(galleryIntent, REQUEST_CODE_IMAGE_GALLERY);
} catch (Exception e) {
e.printStackTrace();
}
}
Then in onActivityResult
if (requestCode == REQUEST_CODE_IMAGE_GALLERY){
if (resultCode == RESULT_OK){
Uri fileuri = intent.getData();
}
}
Use fileUri
to open Image.
Upvotes: 1