How to convert a file:// uri into content:// uri? - android

I have found that on my device, the default media display tool is not showing me the same if Ihave a uri that is:
file://mnt/sdcard/DCIM/Image.jpg
When I go through picking the image with the built in intent I get this:
content://media/external/images/media/247
These both display the same file, but I don't have any sharing options when I use the first one.
My question is, how can I find the content Uri given the file Uri?

I was making the file, so I had a File object file.
So I do this now to get Uri for the file as a "content://" Uri.
Uri capturedImage = Uri.parse(
android.provider.MediaStore.Images.Media.insertImage(
getContentResolver(),
file.getAbsolutePath(), null, null));
Credit to answer on this question How can I capture an image in Android and have it show up in the gallery?

Related

Get filePath for Recents,Downloads,Images from Uri of ACTION_GET_CONTENT Intent android

I am invoking image picker in my android app using intent ACTION_GET_CONTENT.
In onActivityResult() I get Uri using data.getData().
Now I am using an already existing code to compress this image but that requires filePath to the image.
I am getting null while parsing path from Uri for Recents, Downloads, Images.
I use projection MediaStore.Images.Media.DATA on contentResolver.query() with Uri. It fetched correct filePath for all other options except Recents, Downloads, Images.
How can I make these work ? The solution should work on latest Android version.
Uris not working:
Downloads- content://com.android.providers.downloads.documents/document/msf%3A2375
Recents -
content://com.android.providers.media.documents/document/msf%3A2375
Images-
content://com.android.providers.media.documents/document/msf%3A2375
External Storage -
content://com.android.externalstorage.documents/document/primary%3ADownload%2Fimages%20.jpeg
Uri working:
content://media/external/images/media/2165

What is the meaning and how to get the sourceUri and destinationUri in uCrop Library by Yalantis?

I am creating an Image Cropper Activity. However, my app is not working. I do not know what is the sourceUri and destinationUri means in the builder pattern of the uCrop Library by Yalantis. How can we get or find the sourceUri and destinationUri?
I noticed the contributor in Github said that destinationUri is " where you store the Uri. But how can we get the location to store it? For the sourceUri, I getIntent from another activity to get the imageUri.
I get the imageUri from another activity and I make it as the sourceUri. Please kindly correct me and my mistakes. Thank you very much.
File tempFile = new File (String.valueOf(getIntent().getParcelableExtra("ImageUri")));
File tempCropped = new File(getCacheDir(),"tempImgCropped.png");
Uri sourceUri = Uri.fromFile(tempFile);
Uri destinationUri = Uri.fromFile(tempCropped);
UCrop.of(sourceUri,destinationUri)
//.withAspectRatio(1,1)
//.withMaxResultSize(40,40)
.start(this);
I hope that I can get to know what is the meaning of sourceUri and destinationUri and how to get those Uri.
How can we get or find the sourceUri and destinationUri?
The sourceUri as you mentionned is retrieved through your Intent's extras, and it represents the PATH to the image that you are working on.
The destinationUri means the PATH where you want to save your cropped image. It can be on the external storage or anywhere you decide to.

Sharing an Audio wav file

I want to share the audio file in android using but didn't found any working solution.
In My app the app records and save the audio file named as audio_1.wav each time in the app directory and then provide the option for sharing it. So always it share the file with same name(audio_1.wav).
Also checked that it is creating the file correctly.
I tried to share the audio file with below code;
File f = new File(sharePath);
Uri uri = Uri.parse(f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
But while sharing and selecting any app it show unable to attach File.
Also Used FileProvider Class for Nougat Devices but it didn't worked as well.
Checked multiple SO Link as well:
Sharing an audio file
Picking up an audio file android
With regards to the code that you have:
Replace Uri uri = Uri.parse(f.getAbsolutePath()); with Uri uri = Uri.fromFile(f);, to get a valid Uri
Use audio/wav, not audio/*, for the MIME type
Your code will crash on Android 7.0+ with a FileUriExposedException. Use FileProvider for that, where you use <external-path> in your metadata XML resource and add the FLAG_GRANT_READ_URI_PERMISSION flag to your Intent. See the documentation for more.

How to properly put a Uri from a local file into a Intent

I want my local image file (located at res/drawable) to be converted to a Uri, so I can put it into a Intent to start an Activity.
This is what I got until now:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("image/*");
Uri uri = Uri.fromFile(new File("/res/drawable/photo.jpg"));
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.share)));
Then, the activity that receives this intent, displays the image in a ImageView. Something like this:
Uri imageUri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
ImageView myImage = (ImageView) findViewById(R.id.imageView_receive);
myImage.setImageBitmap(bitmap);
But instead of displaying the image, it throws an java.io.FileNotFoundException
java.io.FileNotFoundException: No resource found for: android.resource://com.my.package/drawable/photo.jpg
I searched and tried other ways to get a Uri through my file:
Uri uri = Uri.parse("file:///res/drawable/photo.jpg");
Uri uri = Uri.parse("android.resource://com.my.package/drawable/photo.jpg");
But all of them returned the same error for me.
Why is my image not being recognized?
I want my local image file (located at res/drawable) to be converted to a Uri, so I can put it into a Intent to start an Activity.
Resources are files on your developer machine. They are not files on the device.
This is what I got until now
That will not work, as AFAIK there are zero Android devices with a /res directory off of the filesystem root, and your drawable resources would not be there anyway.
Why is my image not being recognized?
Your third one (android.resource scheme) is the closest. Get rid of the .jpg extension.
However, many third-party apps will not recognize that scheme. And, if you are implementing the activity that is to handle this request, I have no idea why you are using ACTION_SEND. If your objective is to only work within your own app, skip the Uri, package the drawable resource ID (R.drawable.photo) as an int extra, and have your other activity use that resource ID more naturally (e.g., setImageResource() on the ImageView).
You should create ContentProvider for your application, which will allow you to share resources.
Try this lib, it works awesome.
https://github.com/commonsguy/cwac-provider

Android Marshmallow 6.0.1, how do I check if a file is a media one from content URI?

When I share a picture from gallery, in my activity that receives the intent I get the content URI but uri.getAuthority() returns "0#media". The trouble is that even for a PDF, it returns the same value for getAuthority(). How do I distinguish between a media file and another file (eg: PDF) in my activity ?
Am facing this issue only in MM 6.0.1, appreciate your help.
ContentResolver cR = context.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getExtensionFromMimeType(cR.getType(pass_your_URI));
So, if the selected file is an image of jpeg extension, the type returns jpeg. However, just invoking cR.getType(pass_your_URI) will return mime/jpeg which you can also use to differentiate media type out of URI.

Categories

Resources