I have following requirement.
When the App receives a image file from server, it should open the image automatically in default image viewer.
Normal case:
Activity is visible.
Received image file from Server.
App send Intent.ACTION_VIEW
Gallery View shows the downloaded image.
Fail case:
Activity is not visible. (E.g. Press Home and return to launcher.)
Received image file from Server.
App send Intent.ACTION_VIEW
Nothing happens. (<-- Fail)
** If I go back to my App then I can see the Gallery View.
Is there anything I can do to get the Gallery View to show even my activity is not visible?
How i start the Gallery view:
final Intent openfileintent = new Intent();
openfileintent.setAction(android.content.Intent.ACTION_VIEW);
final File file = sharedfile.getFileInstance();
openfileintent.setDataAndType(Uri.fromFile(file), sharedfile.getMimeType());
startActivity(openfileintent);
Have found answer after trial and error.
There is 2 flags to add.
And most importantly, should use application context instead of activity context.
final Intent openfileintent = new Intent();
openfileintent.setAction(android.content.Intent.ACTION_VIEW);
(*1) openfileintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
(*2) openfileintent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
final File file = sharedfile.getFileInstance();
openfileintent.setDataAndType(Uri.fromFile(file), sharedfile.getMimeType());
(*3) getApplicationContext().startActivity(openfileintent);
Related
I use following code to open an image in the gallery:
public void onItemClicked(PictureItem item){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imageUri = FileProvider.getUriForFile(getApplicationContext(), "myapp.fileprovider", new File(item.uri.getPath()));
intent.setDataAndType(imageUri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
This shows my photo in the gallery, but in a 'read-only' mode. I want to be able to delete the image from there, just as if I opened it directly in the gallery.
Which action do I have to use for that? I do not want to use pick, just normal view with the option to delete. I tried ACTION_EDIT but it's not supported (and not quite the right choice neither ...).
I use following code to open an image in the gallery
First, there are hundreds, if not thousands, of "gallery" apps available for Android.
Second, your code simply asks to view an image (with a broken Intent due to your wildcard MIME type). There is no requirement for the app that responds to be a "gallery" app.
I want to be able to delete the image from there, just as if I opened it directly in the gallery.
Then implement that yourself, in your own app, and get rid of the ACTION_VIEW Intent.
Which action do I have to use for that?
There is no Intent action that says "please display this image, but only if you are a gallery app, and, oh, by the way, you must offer a delete option", which appears to be what you want.
In my situation, in Activity A, when clicking a button A, it's supposed to open a pdf file. I can't place the pdf in our server and load it using something like this: https://docs.google.com/viewer?url=https://www.mysite.ca/.../file.pdf. Reason being this: 1) this pdf file is dynamically generated; 2) the path to this pdf is protected.
So I tried this: 1) first save the dynamically generated content (which is retrieved from a web service) in a file in phone; 2) load that saved file in the activity B opened when clicking button A in activity A.
Edited:
In Activity A:
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AppUtils.preparePdfInPhoneAndStartActivity(this, serviceUrl, inputsMap);// the last 2 params are the web service url and inputs
}
});
In preparePdfInPhoneAndStartActivity():
{
...
//== decode the fileContent string - fileContent is a encoded byte string containing the pdf content
byte[] decodedFileContent = Base64.decode(fileContent, Base64.DEFAULT);
String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
fileNameInPhone = dir+"/"+fileNameInPhone;//fileName is the pdf file name, here contacted as the full path, it has this value: /storage/emulated/0/Download/file.pdf
OutputStream out = new FileOutputStream(fileNameInPhone);
out.write(decodedFileContent);
out.close();
File file = new File(fileNameInPhone);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activity.startActivity(intent);// activity is a instance of Activity A.
}
The pdf is opened in second activity (using Intent.ACTION_VIEW). But "Back" to activity A ending up button A frozen (not clickable). The other buttons in activity A still works.
Something I noticed while debugging:
1)the path of the saved pdf file is: /storage/emulated/0/Download/file.pdf. But I couldn't find this file at all in phone at this location: Computer\Galaxy s3(964)\Phone\Download. After unplug the phone and plug it again into my computer, this file shows.
2) log out the app and login again, "button A" is clickable again.
Because of 2) observation, I tried to add this line after launching the second activity:
activity.finish();
This fixes the frozen button A, but when backing from second activity, it doesn't back to the last screen/fragment in activity A, but app hidden in background and bring it up will land the first screen/fragment of activity A. I guess this is because of "activity.finish();"
3) without adding "activity.finish();", when backing to activity A, button A frozen, clicking another menu and selecting the menu that launching the pdf file, it still open the pdf in second activity, but again, the menu is not clickable. From this observation, it appears that the second activity by Intent.ACTION_VIEW is not working properly with a file in phone?
so my questions: 1) is there better way to display the dynamically generated pdf content in Android app without causing this frozen behavior? 2)If "activity.finish();" has to be added after launching second activity by Intent.ACTION_VIEW, how to restore/keep the status of activity A.
Thanks in advance!
Shawn
I'm going to develop a simple app to list .jpg files and after a click call an Intent.ACTION_VIEW, below the code:
File imageFile = new File(filename);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(imageFile), "image/jpeg");
startActivity(i);
The intent works properly, in fact in app I receive the message to select an app to show the selected image. But I noticed that after the choice, for example with Google Photos, not all function are enabled. For example the share function is not visible, has someone already noticed this behavior?
Some other details; if I choose Google Photos I only have the "Info" and "Guide and Feedback" options. If I choose gallery app (I have a Samsung Ace 3) I have only "Info" and "Set as wallpaper" options.
Note: if I open the same image directly from Google Photos all functions are enabled...
The ACTION_VIEW intent calls another application to view the content that you present, in your case a JPEG image. If there is more than on application that can handle that Intent, Android lets the user choose an app to display the image. Once the application receives the Intent, it is up to that application to display the content how it sees fit.
Basically, you have no control over what functions are available with Intent.ACTION_VIEW. If you want more control, you can create your own Activity in which you can view the image and provide whatever functions you would like.
My application is capable of capturing images and saving them (in the public Pictures directory, retrieved by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).
Now, I want to be able to display those images in the system's gallery application. In order to achieve that, I create an Intent like this (filePath is a String containing the image's path):
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filePath));
viewIntent.setDataAndType(uri, "image/jpeg");
startActivity(Intent.createChooser(viewIntent, null));
Displaying an image this way works perfectly fine, but the gallery's Share functionality doesn't seem to work:
When I press the Share-button, I get to choose the app I want to share to; but once I select it, nothing happens, as if the target application does not receive any data.
Is there any explanation why this behaviour occurs and how it can be fixed?
Edit: I used Intent Intercept to capture the Intent created by the Gallery/Photos app:
I have an android app that lists items with url links and on clicking opens the pages using uri,
as below
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
I would like to open only for example a video or image in that page but not the whole of it.
Is that possible in android?
I got what you want to ask.
NO, it is not possible with Intent to open a part of a webpage, it will open entire page,
I suggest you to
1. download and save the image to your drawable folder.
2. create a new actvity , and in the xml associated with that activity add a imageview
3. place your image in that imageview
4. and on the click of the link start the activity with the intent
I would suggest to either use an ImageView (load the image into it - Download image for imageview on Android) or a VideoView (as stream - http://code.tutsplus.com/tutorials/streaming-video-in-android-apps--cms-19888).
It's not possible with an intent.