My app can accept various files e.g. images and such but the problem is if the file such as an image contains illegal characters clicking on it will crash the app. How can such behavior be avoided? if possible I'd still like to be able to have the image open. Thanks.
attachedImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String new_string = imageURI.replaceAll(" ", "%20");
image_uri = Uri.parse("file://" + new_string);
intent.setDataAndType(image_uri, "image/*");
startActivity(intent);
}
});
Related
How to open Instagram app in posting mode (so the same view we get after clicking plus at home screen within Instagram app)? I've seen android apps which are doing that, however wasn't able to find information explaing how to achieve it.
That code works for opening the home view:
Uri uri = Uri.parse("http://instagram.com/");
Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);
likeIng.setPackage("com.instagram.android");
startActivity(likeIng);
Perhaps there is some other uri which could work?
After a bit of research it turned out that deep linking with 'instagram://share' opens the desired view.
You can do that only when you have a media to share on Instagram:
try {
String type = "image/*";
String filename = "/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.instagram.android");
share.setType(type);
File media = new File(mediaPath);
Uri uri = null;
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(share);
} catch (Exception e) {
e.printStackTrace();
}
You can create a fake media though to be able to use this method.
Just to expand on #Paavo answer to show the answer in code.
Uri uri = Uri.parse("instagram://share");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("com.instagram.android");
//Check that the user can open the intent, meaning they have the Instagram app installed
if (getPackageManager().resolveActivity(intent, 0) != null)
startActivity(intent);
else //User doesn't have app installed, handle however you want
throw new Resources.NotFoundException();
In my Application I try to share an Image with the Intent (Intent.ActionSend).
For the guaranteed Access I use a Fileprovider which creates the Uri to the file.
Under Android Nougat my code works but with Marshmallow not. It shows no errors but the Image is not sent to the recieving App.
The Output from VisualStudio also shows no errors or somthing like that.
I couldn't find any answer online so far. What is the problem with Marshmallow?
public void SendPhoto(IEnumerable<string> photos, string text)
{
var files = new List<Android.Net.Uri>();
//creates for every photo an own ContentURI
foreach (var path in photos)
{
var file = new Java.IO.File(path);
files.Add(FileProvider.GetUriForFile(Context, "com.testapp.fileprovider", file));
}
//Checks if Photolist are empty
if (!files.Any())
throw new Exception("Photos not found");
Intent intent = new Intent(Intent.ActionSend);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
intent.SetType("image/jpeg");
intent.PutExtra(Intent.ExtraSubject, "Subject");
intent.PutExtra(Intent.ExtraStream, files.First());
//creates the sharemessage
var shareMessage = files.Count == 1 ? "Send photo from TestApp" : "Send photos from TestApp";
//start Intent chooser
Context.StartActivity(Intent.CreateChooser(intent, shareMessage));
}
I have a share button in my app,Instead of sending app link http://play.google.com/store/apps/details?id=<package_name> via intent ACTION_SEND I want to link, app link in play store to a text(app name).so receiver will see a name and by clicking on that will be redirect to app in play store.How can I achive that?
SpannableString s = new SpannableString("http://play.google.com/store/apps/details?id={packageName}");
Linkify.addLinks(s, Linkify.ALL);
String messStr = "Check out this event: " + eventObj.getString(Configs.EVENTS_TITLE) + " | from #AppName "+"\n"+s +"\n";
Bitmap bitmap = ((BitmapDrawable) eImg.getDrawable()).getBitmap();
Uri uri = getImageUri(EventDetails.this, bitmap);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.putExtra(Intent.EXTRA_TEXT, messStr);
startActivity(Intent.createChooser(intent, "Share on..."));
Updated Version: User change the package name
You have the button, and can set the text to whatever you want, then you set the onClick method with intent to the play store
///You need to set an EditText attribute in your layout, similar to how you set the button
packageName = (EditText) findViewById(R.id.edit);
Button button = (Button) findViewById(R.id.button_id);
//locates button from xml layout file
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Tries to open the play store to your listing
String str = packagename.getText().toString();
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + str)));
}
catch (ActivityNotFoundException e1)
{
//catches error if play store isn't found
Toast.makeText(this, "Play Store not installed", Toast.LENGTH_SHORT).show();
}
}
});
This should work in theory.
i've searched a lot but did not stumble upon something like i have to do.
So when i click to send email file is shown as attached but i never receive attached file
Is there some special way to send attachments that you receive as URL?
Here is my activity code: http://pastebin.com/uzdJYxab[2]
And here is my project https://docs.google.com/file/d/0B-91m-6ZevwCRTYtYXRGb3l6UVE/edit?usp=sharing
Code for "Send email" button:
sendEmail_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_SUBJECT, "Attachment from app");
intent.putExtra(Intent.EXTRA_TEXT, "Sending mp3 file " + title);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"some_email#email.com"});
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(new File(trackUrl)));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(intent, "Send mail"));
}
});
Maybe there has to be some other way to put audio file from URL to intent? I do have to use intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uri); as a part of my task. However i skipped another part
ACTION_SEND_MULTIPLE would use external activity which should access sound file. To allow such access you need: - Create ContentProvider. - Override public ParcelFileDescriptor openFile(Uri uri, String mode)
Does this mean that only with a use of content provider can i get and put file from url as email attachment? I googled but did not find how to correctly override public ParcelFileDescriptor openFile(Uri uri, String mode), maybe someone will at least point me in right direction? Again i'm not asking to solve this, but at least point me out my mistakes and give some advise.
You don't need a content provider if the files are in a public directory. I do it in this way :
private void launchShareActivity(final List<String> exported_filenames) {
Log.v(DEBUG_TAG, "launchShareActivity");
final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("*/*");
if (exported_filenames.size() == 0) {
Log.i(DEBUG_TAG, "there no file to share");
return;
}
final ArrayList<Uri> uris = new ArrayList<Uri>();
for (final String exported_file : exported_filenames) {
final File f = new File(exported_file);
uris.add(Uri.fromFile(f));
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
activity.startActivity(Intent.createChooser(intent, "Send page"));
}
I'm trying to implement "set as" functionality for images. I'm using Intent.ATTACH_DATA so users can at least choose contact photo and wallpaper. The extras I should pass confuse me. If I read the documentation right,
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setType("image/*");
intent.setData(mImageCaptureUri);
startActivity(Intent.createChooser(intent, "hey"));
Should be all. This works for wallpapers, but with megapixel data, the app crashes, because no crop activity could be found. Does someone have a working example? The official gallery app does manage to find the camera.crop activity...
A general hint on where to find elaborate system intent documentation is welcome as well.
After a long and winding road through the android source, I found the actual code in the default gallery (gallery3d) app. I adapted for use in my own application, and rewrote it again for convenience when importing in other applications. If you use or appreciate this, I ask that you upvote this answer.
Adapted from : gallery3d source at grepcode
Usage: change first line to match the full path (starting with /mnt/) of your photo.
add string "set_as" to your strings.xml as the action chooser title.
String absolutepath = MyApplication.appRootDir + relpath;//change for your application
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = absolutepath.substring(absolutepath.lastIndexOf('.') + 1);
String mimeType = map.getMimeTypeFromExtension(ext);
Uri uri = Uri.fromFile(new File(absolutepath));
intent.setDataAndType(uri, mimeType);
intent.putExtra("mimeType", mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Activity activity = (Activity) this;
activity.startActivity(Intent.createChooser(
intent, activity.getString(R.string.set_as)));
Above answers are great , however here is one i tested and used.
private void setAsWallpaper(String path_of_file) {
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_ATTACH_DATA);
File file = new File(path_of_file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.setDataAndType(FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file), getMimeType(path_of_file);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Exception generated", Toast.LENGTH_SHORT).show();
}
}
private static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
and simply call setAsWallpaper(path);
here path is absolute path of file .