Intent Share Chooser not showing correct names - android

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?

Related

"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

Video sent to whatsapp not playing

In my app there is a feature where the user can share a video with the app of their choice. The code is fairly straightforward (where mediaPath is a variable of type String which is a path to a valid video):
File media = new File(mediaPath);
Uri uri = FileProvider.getUriForFile(context, getString(R.string.file_provider_authority), media);
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("video/*");
String title = getString(R.string.share_video_title);
Intent chooser = Intent.createChooser(share, title);
if (share.resolveActivity(context.getPackageManager()) != null) {
startActivity(chooser);
}
Sharing works perfectly on gmail (for example) and seems to work fine on whatsapp as well. It compresses the video and uploads it. The recipient gets the video and is able to see a thumbnail and download it. However they cannot play the video.
I finally found the solution is here
public void shareVideoWhatsApp() {
Uri uri = Uri.fromFile(v);
Intent videoshare = new Intent(Intent.ACTION_SEND);
videoshare.setType("*/*");
videoshare.setPackage("com.whatsapp");
videoshare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
videoshare.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(videoshare);
}
Refrence
Are you try this:
String path = ""; //should be local path of downloaded video
ContentValues content = new ContentValues(4);
content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, path);
ContentResolver resolver = getApplicationContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("video/*");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Hey this is the video subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Hey this is the video text");
sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share Video");

Send Image as email attachement in android

I am getting an error that i cannot attach empty file in gmail.I am trying to build simple app in which when i click on the button it show choices by which i can send image,But the below code is not working.
Please help i am novice in android.
code:
if(view.getId()==R.id.SendImage)
{
Uri imageUri = Uri.parse("android:resource://com.example.jaspreet.intentstest.drawable/"+R.drawable.image);
intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_STREAM,imageUri);
intent.putExtra(Intent.EXTRA_TEXT,"Hey i have attached this image");
chooser=Intent.createChooser(intent,"Send Image");
startActivity(chooser);
}
Try using this:
shareIntent.setType("image/png");
By using this the intent know that it will send .png file.
On this link you can find a list of all media type/subtypes
Try this
Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
Try this snippet
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);
startActivity(Intent.createChooser(share, "Select"));
An android:resource:// is not a File, and probably you are messing up your Uri by converting to a File and then back to a Uri.

Android MediaStore can't get inserted image data

I have image editing app with image export function. Previously I tryed to export it directly by passing file path uri to ACTION_SEND intent. Something like:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.export)));
But not all apps can correctly parse such intent. So now I first prepare uri by adding image to android's MediaStore:
shareUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), projectName));
...and then passing shareUri to intent. Now all apps can correctly process this intent, but one problem appear - each time when user exports image its copy added to android MediaStore(even if image path and name the same).
I trying to determine is image already added to mediastore by following code:
Cursor c = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media.DATA + "=?",
new String[] { filePath }, null);
if (c.getCount() > 0 && c.moveToFirst()) {
shareUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"" + c.getInt(c.getColumnIndex(MediaStore.Images.Media._ID)));
c.close();
} else {
shareUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), projectName));
}
...but c.getCount() always == 0. Even if I copy saved uri from MediaStore.Images.Media.insertImage and query it directly by
Cursor c = getContentResolver().query(savedUri, null, null, null);
How I can detect is image file already in MediaStore?
Found solution: not using MediaStore.Images.Media.insertImage to add file to gallery but doing similar by MediaScannerConnection:
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{file.getAbsolutePath()},
new String[]{"image/*"},
new OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.export)));
}
});

Cannot insert image into Media database

The following code allows me to take pictures, however it does not insert the image into the db. What am I missing?
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
values.put(Media.TITLE, "Image");
values.put(Images.Media.BUCKET_ID, "292");
values.put(Images.Media.BUCKET_DISPLAY_NAME, "thename");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Media.DESCRIPTION, "Image capture by camera");
values.put("_data", IMG_FILE_PATH);
Uri uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
i.putExtra("return-data", true);
startActivityForResult(i,REQ_PHOTO);
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(imageIntent, REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY);
Try just using this code. It seems like you overwriting _data is causing issues when Android tries to store the image.

Categories

Resources