Strange issue.... when I execute an ActionView intent to view whith default gallery app an image located for example in Download folder it's enough to set the correct mime type without the need to specify file extension.
For example look this c# code for xamarin:
string myImage = Path.Combine(Environment.ExternalStorageDirectory.Path, Environment.DirectoryDownloads, "myImage")
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Uri.FromFile(new File(myImage)), "image/jpeg");
StartActivity(intent);
This works fine even if myImage is a jpeg file with no extension (normally I should use myImage.jpg).
But if I want to open the same image located in cache, using FileProvider to give correct permission, it doesn't work until I explicitly set the .jpg estension.
//Assuming myImage is a valid jpeg just present in cache
string myImage= Path.Combine(CacheDir.AbsolutePath, "myImage");
Uri imageUri = Android.Support.V4.Content.FileProvider.GetUriForFile(this, "com.myapp.fileprovider", new File(myImage));
Intent intent = new Intent(Intent.ActionView);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
intent.SetDataAndType(imageUri, "image/jpeg");
StartActivity(intent);
This does not work, gallery says can't find element.
But if I rename both file and content variable in myImage.jpg the gallery shows the image correctly.
Since I have to download files of several mime types but with no name I would like to rely only to the specific type avoiding to append file extension.
Has anyone faced the same problem?
Related
I am new to android development.
I am trying to open the default gallery app to view an image like this:
Uri u = Uri.parse((String) gridView.getAdapter().getItem(position);
Intent i = new Intent();
i.setDataAndType(u, "image/*");
i.setAction(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
I provide the file paths to my images like this:
/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20191023-WA0045.jpg
/storage/emulated/0/Pictures/9GAG/1565987862000.jpg
My App and Google Photos can display the image given this path, the Samsung Gallery( tested on Android Oreo) or the Xiaomi Gallery App (tested on Android X) can't find the image; also if i try to add "content://" or "file://" at the beginning.
I suspect that the apps want to have a Content URI or some kind of symbolic link without the "/storage/emulated/0/" part instead of a File URI. I searched for methods to get the content URI out of the file path, but didn't find any examples for Android X.
So what is the correct URI to pass on through the Intent and how do i get it?
Or maybe somebody knows a better way to just open the gallery app to a specific picture i provide by an absolute file path (Both External Storage and SD Card)?
Thank you in advance and greetings!
copied from this question here , this might work for you
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
I want to open the default gallery app to a folder (named fooo) in the primary storage. I thought the easiest way would be to use Intents. When I use this code for my intent, the gallery app opens but it shows no image. I check with file explorer and there are images.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/fooo/");
intent.setDataAndType(uri, "image/*");
startActivity(intent);
First, there is no requirement for any ACTION_VIEW activity to be able to handle a directory as the Uri.
Second, your MIME type is incorrect, as a directory is not an image.
Third, your Uri is invalid, because it lacks a scheme. Replace:
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/fooo/");
with:
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "fooo");
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
i am trying to set ImageView as resource of uri.pare.
to perform this task i tried this but it's not working: failed to load image
method 1:
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(Uri.parse("R.id.imageView1"),"image/*");
intent.putExtra("mimeType", "image/*");
startActivity(Intent.createChooser(intent, "Set as:"));
method 2:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("R.id.imageView1"),"image/*");
startActivity(intent);
i am not able to set ImageView as resource of uri.parse, so please help me how do i set ImageView as resource of uri.parse
i found a code but i am not understanding how to adjust this code with my code:
this is i found:
ImageView imgView = (ImageView) findViewById(R.id.image3);
imgView.setImageURI(Uri.parse("file://mnt/sdcard/d2.jpg"));
how do i adjust it with my code:
intent.setDataAndType(Uri.parse("R.id.imageView1"),"image/*");
ImageView.setImageURI only takes Android content URI as stated here:
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/jupslaeAEuo
In other words, it takes things like "file://" or "content://" URI's, not resource ID's like what you are trying to do.
If you want to do that, the only way that I know how, and what I did, was to write the resource to a file and then pass the file path to the ImageView URI. There are many examples of how to write files to the system (including thinking about using the SDCARD, etc.)
I'm pretty sure you could write a ContentProvider that will resolve the "content://" or "file://" schemes using the ContentProvider openFile / openInputStream methods, but I abandoned that before I needed it. I might finish it - if anyone is curious how it works...
"Setting uri.parse resource as ImageView" is not works and i failed to found such a way to
"Set uri.parse resource as ImageView"
the simple and working way is to save image to sd card then use code like this:
intent.setDataAndType(Uri.parse("file://mnt/sdcard/temp_image0.png"),"image/*");
see this post to find out saving image to sd card:
how to save ViewPager Current image to sd card on button click
I have a Bitmap saved in the directory: Environment.getExternalStorageDirectory()+ File.separator + "Images" in a subdirectory. With a fileManager i can find the image and open it. But in the Gallery of the Smartphone i can't find the Image.
How can i get it there?
To be make you file known by the Gallery you need to call the MediaScanner:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
See also: https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/
You can actually directly access the Pictures directory. To do this, use
getExternalStoragePublicDirectory(String type)
, where type is equal to Environment.DIRECTORY_PICTURES.
See http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29 for more info.
That's cleaner than supposing that pictures are always stored in a directory called "Images"...
Now to force the Gallery to register the change, look at https://stackoverflow.com/a/15837638/2984973 .
To quote the answer, you want to broadcast an intent specifying your new picture, likewise:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));