How to use from my app another app to visualize an image? - android

I want to program an app. In this app, the user selects an image file path and the app opens another app (Google Photos) to show that image. This image is in the public folder Downloads.
I am using Android 9.0.
What I tried:
File imageFile = new File("/storage/emulated/0/DCIM/Camera/IMG_20220203_114716.jpg");
Uri uri = Uri.fromFile(imageFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
And in AndroidManifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I get a FileUriExposedException.

SOLVED:
Uri collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
String[] projection = new String[]{MediaStore.Files.FileColumns._ID};
String selection = MediaStore.Files.FileColumns.DATA + "=?";
String[] selectionArgs = new String[]{"/storage/emulated/0/DCIM/Camera/IMG_20220203_114716.jpg"};
String sortOrder = null;
Cursor cursor = getApplicationContext().getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder);
cursor.moveToFirst();
Uri uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL, cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setPackage("com.google.android.apps.photos");
intent.setDataAndType(uri, "image/*");
startActivity(intent);

Related

Intent Share Chooser not showing correct names

I first created a new pdf file using:
ContentValues values = new ContentValues();
values.put(MediaStore.Files.FileColumns.DISPLAY_NAME, file_name);
values.put(MediaStore.Files.FileColumns.MIME_TYPE, "application/pdf");
values.put(MediaStore.Files.FileColumns.RELATIVE_PATH, "Documents");
ContentResolver resolver = context.getContentResolver();
Uri uri = resolver.insert(MediaStore.Files.getContentUri("external"), values);
fos = resolver.openOutputStream(uri);
document.writeTo(fos);
fos.flush();
fos.close();
values.clear();
resolver.update(uri, values, null, null);
Now I pass this "uri" to different activity where I choose Intent.Action_Send to share pdf file to say whatsapp using:
Intent intentShareFile;
intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.setType("application/pdf");
startActivityForResult(Intent.createChooser(intentShareFile, "Share File"), FINISH_SHARING);
This open the intent chooser window with multiple apps, whatsapp, facebook, but shows the number "39913" from the uri "content://media/external/file/39913" instead of file name. I expect to show my file name here "New.pdf". Can you please solve the problem?

Android : use of mediaStore with pdf file

I try to use media store to store a pdf download (in Environment.DIRECTORY_DOWNLOADS) from a private network and to show it. I want the user to be able to choose the application to show it.
In my code, I have difficulty to define the file path "dataFile"
Can you give advice, thanks
Laurent
String fileType="application/pdf";
String filetxt="Choose pdf Application";
final Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(dataFile, fileType);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intentChooser = Intent.createChooser(intent,filetxt);
context.startActivity(intentChooser);
dataFile should be URI, which can be easily created from File (or its path)
File file = new File(pathToFile);
Uri dataFile = Uri.parse(file.toString()); // or strightly pass pathToFile here
My solution after several tests :
String[] projections = new String[]{
MediaStore.DownloadColumns._ID,
};
String selection =MediaStore.Files.FileColumns.DISPLAY_NAME+"=?";
String[] selectionArgs = new String[]{newLocalpath};
Cursor cursor = context.getContentResolver().query(
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
projections,
selection,
selectionArgs,
null
);
Uri uri=null;
int docIndex = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns._ID);
if (cursor.moveToNext()==true) {
uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cursor.getInt(docIndex));
}
if (uri!=null) {
try {
String fileType="application/pdf";
String filetxt="Choose pdf Application";
final Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(uri, fileType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent intentChooser = Intent.createChooser(intent,filetxt);
intentChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentChooser);
} catch (Exception e) {
Log.d("application", "error starting download file: " + e.toString());
}

"Unable to find image" when sharing using Instagram

in my application the user has the option to share an image on Instagram.
This is the code i'm using :
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.TITLE, MediaStore.Images.ImageColumns.DATE_TAKEN};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
shareIntent.putExtra(Intent.EXTRA_STREAM, photoUri);
shareIntent.setPackage("com.instagram.android");
c1.close();
startActivity(shareIntent);
The problem is that it shows a message on the app saying
Unable to load image.
I added this permission in my manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
i also made sure to give permission from my device but nothing is working.
i solved my problem. i removed this line of code
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
and i replaced this
shareIntent.putExtra(Intent.EXTRA_STREAM, photoUri);
with this
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photoPath, "img", "Identified image")));
try this code
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
createInstagramIntent(type, mediaPath);
private void createInstagramIntent(String type, String mediaPath){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
supported Photo Format are jpeg, gif, png

Android open gallery from folder

I want to show a photo in android gallery, and be able to slide throw the others photos on that folder.
Intent intent = new Intent(Intent.ACTION_VIEW);
File f = new File(path);
intent.setDataAndType(Uri.parse("file://" + f.getAbsolutePath()), "image/*");
mContext.startActivity(intent);
thats how i am doing it now, but wont let me slide throw the rest of the images in the folder.
i tried:
How to open one particular folder from gallery in android?
Built-in gallery in specific folder
Gallery with folder filter
Without any luck.
i would be really happy if someone have the solution.
Thanks!
Try This
Intent i=new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "image/*");
startActivity(i);
See these Links
How can I use Intent.ACTION_VIEW to view the contents of a folder?
Android ACTION_VIEW Multiple Images
Java and Android: How to open several files with an Intent?
if this solves your problem. Also check
https://www.google.co.in/?gfe_rd=cr&ei=c5n9U6ruE7DO8gfXz4G4BA&gws_rd=ssl#q=view+like+gallery
also check Gallery widget
This question is from five years ago, However I want to give an answer that worked for me instead of the correct answer.
In order to show the photo and slide through the others photos, we need to give to the intent not the file uri but the media uri.
public void ShowPhoto(File imageFile) {
String mediaId = "";
String[] projection = new String[] {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME
};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
while (cursor != null && cursor.moveToNext()) {
String name = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)));
if(name.equals(imageFile.getName())){
mediaId = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)));
break;
}
}
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if(!mediaId.equals("")){
mediaUri = mediaUri.buildUpon()
.authority("media")
.appendPath(mediaId)
.build();
}
Log.d("TagInfo","Uri: "+mediaUri);
Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
startActivity(intent);
}
Try this way,hope this will help you to solve your problem.
final int OPEN_GALLERY = 1
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), OPEN_GALLERY);

how to open native contact card by URI

Can someone please explain me how to open contact native for specific contact URi
I manage to resolve the contact URI by:
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(displayName));
and then I tried do this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT, "555");
context.startActivity(intent);
but it's only open the native address book without any resolving
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
context.startActivity(intent);
You can retrieve the contactId by browsing using contentResolver:
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.d(tag, "Id: "+ id+"\t Name: "+name);
After you get the cursor of contacts and the index of a particular contact, get the Uri and start activity with intent of ACTION_VIEW as below:
cursor.moveToPosition(position); // or cursor.moveToFirst() if single contact was selected.
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(ContactsContract.Contacts.getLookupUri(id, lookupKey));
try {
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
The accepted answer does not work when Android Messages is not the user's default SMS app. For some reason the intent goes through Android Messages before opening the contact details, and when the app is not default it takes you to their SMS permissions activity.
The below intent uses a slightly different URI: CONTENT_LOOKUP_URI instead of CONTENT_URI, and it works no matter which texting app the user is using.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, String.valueOf(contactID));
intent.setData(uri);
context.startActivity(intent);

Categories

Resources