How to open an Image without saving another Location? - android

now file:// uri are not supported. people say to use FileProvider but i think i have to save image in app data location to use this. is there a way to get content:// URI Without Saving image in data directory.i want to open image in sd card from gallery. this below code works on old phones, after android 7 it is not working. FileUriExposedException example image file is in dcim folder my old code is this
File pic2toview = new File(imagepath);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(pic2toview), "image/*");
startActivity(i);

Code to select an image from gallery by intent,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST);
Then it handled in onActivityResult,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
imageUrl = data.getData();
}
}
Here imageUrl is Uri of image selected from gallery.

Related

How to share a video from stored in raw folder of my app - Android studio

I want to share a video that is stored inside the raw folder of my Android app. I saved this video file while developing my app. Now I want to share this by clicking a button on WhatsApp. How can I do this? Till now I can share video files by selecting from the gallery -
private void selectVideo(){
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"), REQUEST_TAKE_GALLERY_VIDEO);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
Uri uri = data.getData();
phone = "+91xxxxxxxx";
Intent videoshare = new Intent(Intent.ACTION_SEND);
videoshare.setType("video/*");
videoshare.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
videoshare.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(videoshare);
}
}
}
You can copy the file from raw resource to device storage and then use FileProvider to serve your file.
You could also use your own 'file'provider or RawProvider (extend ContentProvider) to serve the file directly from raw resource.
There are examples on internet for an AssetsProvider which serves directly files from assets.

How Can I Get the Uri of Image if I Open the Gallery and Select Image and share with my App

How Can I Get the Uri of Image if I Open the Gallery and Select Image and share with my App, but Image is not showing in ImageView
Open gallery for getting Image Uri
private static final int REQUEST_SELECT_IMAGE = 100;
private void handleOpenGallery() {
Intent intentSelect = new Intent(Intent.ACTION_GET_CONTENT);
intentSelect.setType("image/*");
startActivityForResult(Intent.createChooser(intentSelect, getString(R.string.select_picture)),
REQUEST_SELECT_IMAGE);
}
Uri of selected Image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
shareImage(uri);
}
}
}
Share Image
private void shareImage(Uri imageUri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share to"));
}
You can follow below code by Raghu to open gallery,
if you only want to open gallery then you can use,
Intent intentSelect = new Intent(Intent.ACTION_PICK);
Add check for read/write permissions if OS version is above Marshmallow(6.0)
If you still don't get the uri then in onActivityResult when you will fetch the uri try the getPath method from here
Add check if the Android OS version is above Nougat(7.0) or not, as above nougat while fetching file uri you may get "FileUriExposedException" refer this link for more info

Intents for File Picker

I can find some very old file-picker samples, but think it might now be possible to do it with the more up to date Intent.
I want to load and save files. It will be a text file but with an extension .mfl (an extension I am using for my files).
I have seen someone's code for images (but just for loading):
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
What do I need in setType to only see my .mfl files?
I already have a onActivityResult doing other stuff, so need to create my own requestCode. I was thinking
int PICK_MYFILE_REQUEST = 1001;
So I think my onActivityResult needs to look like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(/* doing my other stuff */){
} else if (requestCode == PICK_MYFILE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
/* load the file */
} catch (IOException e) {
e.printStackTrace();
}
}
}
As you can see I think I might be almost there, but not completely sure what I am meant to be doing. (I am new to Android - I think you can guess.)
Is this roughly right? What do I need to do to get this working?
Also, how do I modify this to get a file saver dialog?
Try this :
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
For custom type you can use application/mfl so your Intent should be:
//Use ACTION_OPEN_DOCUMENT to get read/write permission
Intent intent = new Intent(Intent.Intent.ACTION_OPEN_DOCUMENT);
//"application/mfl" to request only .mfl files
intent.setType("application/mfl");
//Request readable files
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(Intent.createChooser(intent, "Select File"),PICK_MYFILE_REQUEST);

image not cropping after cropped

I am attempting to crop an image using intent after the image is selected from the gallery. Here is my snippet of code
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
//******code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
Here I am calling the above snippet with PICK_IMAGE_REQUEST intent handle
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
try {
Uri filePath = data.getData();
what could be wrong since I am using the same intent after cropping which is PICK_IMAGE_REQUEST
There are no documented and supported "crop" extras for ACTION_GET_CONTENT or any other standard Android Intent. Nor is there a documented and supported standard Intent action for cropping. There is no requirement for any device to have apps that support undocumented and unsupported extras, actions, etc.
If you want to crop an image, use any one of a number of existing open source libraries for image cropping.

(android) taking picture without saving

I used the code below to call camera in my android app.
With the code, I could take a picture and get the absolute path of the picture.
But the problem is that the absolute path always pointed the internal storage.
That is, the phone automatically saved the picture I took and I had to erase them one by one.
I just want the absolute path so that I can turn it into a File object without saving in my storage or SD card.
Maybe, I can temporarily save it in my allocated memory.
Is there any way to do this?
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK)
return;
if(requestCode == PICK_FROM_CAMERA){
imageUri = data.getData();
Log.d("메시지", "uri = "+imageUri);
Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
c.moveToNext();
absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
Glide.with(this).load(imageUri).into(image);
You can take a thumbnail of image without store into internal memory or SD card:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
For full sized image you need to define shared file (for example: cache file at SD card) and put it as MediaStore.EXTRA_OUTPUT:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// camera app will save photo into sharedFileURI path
intent.putExtra(MediaStore.EXTRA_OUTPUT, sharedFileURI);
startActivityForResult(intent, PICK_FROM_CAMERA);
See Save the Full-size Photo article of official documentation for more info

Categories

Resources