Am trying to create a homescreen shortcut programmatically on android. So far I've been able to add the shortcut itself with the following code:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(mContext, mContext.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld 123");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.icon);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
mContext.sendBroadcast(addIntent);
But, the shortcut is installed using the default icon in my resources. However, I would like to fetch icons from my website and adding an icon to the shortcut.
First, I need to download this shortcut. Under the assumption that I have this done, and the icon is on the sdcard for example, I have been unable to set an drawable icon.
The following code:
try {
Uri contentURI = Uri.parse("http://mnt/sdcard/mytest/test.png");
ContentResolver cr = mContext.getContentResolver();
InputStream in;
in = cr.openInputStream(contentURI);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=8;
Bitmap thumb = BitmapFactory.decodeStream(in,null,options);
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(mContext, mContext.getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.putExtra("someParameter", "HelloWorld 123");
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, thumb);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
mContext.sendBroadcast(addIntent);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
The file definitely exist and I've verified that using adb shell... This piece of code shows the following error:
10-13 16:11:31.184: WARN/System.err(23273): java.io.FileNotFoundException: No content provider: /mnt/sdcard/mytest/test.png
What am I doing wrong?
Thanks
You are trying to get bitmap from local resources (by using content provider).
To download Bitmap from server you should follow this:
Why is this image bitmap not downloading in Android?
It seems like your application unable to access test.png. Make sure it exists. Maybe you can start with local storage rather than sd card.
Related
I am developing an app, where I want to share the TEXT and IMAGE to other apps installed on my phone.
But when I click the share button Neither it shares TEXT nor IMAGE just empty it directs me to another app I chose for sharing.
below postDescription and postImage are my methods of model class, and I checked that am I getting values are not in a toast it gives there values properly.
Below is the code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, postDescription);
Uri uri = Uri.parse(postImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(shareIntent, "Share With"));
So the above bunch of code was not working, then I found code through which I can share my post TEXT and IMAGE to WhatsApp only, I tried that but it shows file formate not supported inside WhatsApp.
below is the code for sharing to WhatsApp only:
Uri imgUri = Uri.parse(postImage);
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}
I want to know how to create a share button that works fine for WhatsApp and everything or only WhatsApp will do the work for me.
By removing the package from the intent, Android should display the list of apps that can handle the type of intent.
Uri imgUri = Uri.parse(postImage); //Provide the URI to the downloaded image, not an external URL
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("*/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
if (whatsappIntent.resolveActivity(packageManager) != null) {
startActivity(whatsappIntent)
}
I am working on a device running Android 8.1.0. I'm trying to open an image from a message attachment. I had a cache of image files working using a FileProvider a week ago and now it just stopped working without me touching the code. I'm trying to share an image from my internal app storage to Intent.ACTION_VIEW outside of my app. The photo viewer does launch, but there's a progress circle that just keeps spinning. Any suggestions? Thanks!
void launchViewer(File f) {
Uri uri = FileProvider.getUriForFile(context, "com.company.secure.provider", f);
Intent intent = new Intent(Intent.ACTION_VIEW);
String mimeType = Attachment.getMimeType(f.getName());
//TODO Test to make sure this works on all devices...
if (mimeType.startsWith("video")) {
mimeType = "video/*";
}
if (mimeType.startsWith("image")) {
mimeType = "image/*";
}
if(mimeType==null || mimeType.length()==0) {
unknownMimeType(f.getName());
return;
}
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if(resInfoList==null || resInfoList.size()==0)
{
AlertDialog dialog = new AlertDialog.Builder(context).setTitle("Error")
.setMessage(String.format("Cannot find app to open file type(%s)",mimeType)).show();
return;
}
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setDataAndType(uri, mimeType);
context.startActivity(intent);
}
So I just had to add the following line right before starting the activity.
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
For some reason, the APK I shipped to testing that doesn't have this line in it works... but any new build does not work without this. I read that using the ResolveInfo method will only work for Lollipop and below and you will have to grant the permission directly to the intent. (Not sure how true this is). For this reason I left both permission granting methods in there to cover all bases. Thanks for all the help!
I am trying to share an image from my Android app. I am trying to send it as an Email attachment as well as a photo on WhatsApp.
The code is:
String imageUrl = Path to image (eg. sdcard/pictures/image1.jpg);
shareImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri uriToImage= Uri.parse(imageUrl);
Log.d("Image", ""+uriToImage);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image:"));
}
});
What's happening is:
On WhatsApp I can share the image easily.
On Gmail, it says that the attachment could not be sent.
On Hangouts I get a toast which says Photo couldn't be found
On Facebook too, the post is not accompanied with the Image but I can post.
On Facebook Messenger, it crashes without opening.
The tutorial that I have followed for this is given here. The send binary content part of the tutorial is the one that I have implemented.
Another thing I tried was to set the image in an ImageView and see if it is displayed. The image is displayed correctly. Also, the log message prints the correct path of the image.
I also read and tried the answers to: Question 1 and Question 2 but to no avail.
Where am I going wrong?
try this,
try
{
File myFile = new File(share_image_path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
Yes the #CommonsWare Answer is correct you missed the Schema in your Intent.putExtra() and that's why it is failing to read your image in other social media platform
Here's my solution
Uri fileUri = Uri.fromFile(new File(imagePath));
//No need to do mimeType work or ext
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Image:"));
BTW it works on all the mentioned platform
Uri image_uri = Uri.parse("file://"+imagePath);
Another method of creating Uri and passing it to intent
I'm using MediaStore.ACTION_IMAGE_CAPTURE to take a picture with MediaStore.EXTRA_OUTPUT to point to the Uri for the DCIM/Camera directory where all the other photo/video files are stored.
The photo file is successfully taken and I can see it using ES File Explorer and can view it inside my app. However it is not shown in the gallery when I use Intent.ACTION_PICK
Intent selectPictureIntent = new Intent(Intent.ACTION_PICK);
selectPictureIntent.setType("image/*");
I've read the other topics on updating the Gallery after the picture comes back using
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
and also
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
What's going on here :(
why did you use LocalBroadcastManager? LocalBroadcastManager can only send broadcast data inside of your app. Try to use
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, _outputMediaUri);
sendBroadcast(intent);
Gallery always listens to URI change. After file event broadcast to Gallery, the data stored in Gallery will be updated.
it also can problem with KITKAT build version.. for sure you can use this code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(out); \\out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
I've wrote content provider to open a png file in my app package with an external application (standard image viewer of Android). Image is stored in asset folder.
I cannot understand where is a problem, but it doesn't work for me.
openFile of ContentProvider:
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
URI file_uri = URI.create("file:///data/data/com.package/assets/image.png");
File file = new File(file_uri.getPath());
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
}
Starting activity:
Uri uri = Uri.parse("file:///android_asset/image.png");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
Is this approach correct and where is my mistake? Or am I totally wrong?
The new Activity doesn't have access to your internal assets directory. You can either put the image on the sdcard or use your own ImageView that is part of your application.