Android load image from url and save to phone - android

Currently I have the following:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(mediaLoc)), "image/" + type);
context.startActivity(intent);
This was because the app used to download images from urls and saved them into the sdcard before viewing. But now I want to change the implementation to show the image from a url in the default image viewer when ACTION_VIEW is called and give them the option to save. Is this possible with ACTION_VIEW? I'm asking because I wish to use the default image viewer in the phone as it has already handled the zoom functions.

You would have to make a custom action_view for it.

Related

Android - how to open image in gallery with delete option

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.

Displaying Images in Gallery and sharing them

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:

send raw image or binary data directly to the other application

I would like to ask if there is a possibility to send raw data to the other application? Example: taking pictures from a custom camera and send to other application. I observe that using intent is just only sending data which is already saved to storage, but i want that let the 2nd application decided either they save the image or discard. Just like in the tutorial of on how to make custom camera but in that instance, only one application running and only startActivityForResult and the listener which is onResultActivity. In my case, I want that from my custom camera, pass the raw data to the other application and let the other application manage the raw image receive. I call this raw image because this image is in the buffer and not on the storage. Is that possible to pass the raw image/data to other application?
You would need to save the file and pass the URI to the file via an Intent.
Uri image = Uri.parse(fileName);
Intent myIntent = new Intent(Intent.ACTION_VIEW, image);
myIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(myIntent, "Open Image");
startActivity(chooserIntent);

ACTION_GET_CONTENT to Google Images

As the docs say: "ACTION_GET_CONTENT could allow the user to browse over the web and download the desired data".
And that's what I need, from Google Images. Although, if I use this intent's type the app crashes (I suppose it only expects ACTION_VIEW to open the browser):
Intent intent = new Intent();
intent.setData(Uri.parse(url));
intent.putExtra("return-data", true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE);
It needs to (edited to clarify what I need):
Estabilish the connection to the web browser
The URL to Google Images
Retrieve the image's URL after the user has chosen one (it needs to close the browser, the ACTION_VIEW just keeps on going until the user decides to close it and does not retrieve any information).
As far the URL I have is this one, but it needs something else because it is going to a default search, not for images:
String url = "http://images.google.com/search?q=" + imagename;
I appreciate any help.
Try using the following url. It works fine for me in my app:
"http://images.google.com/search?num=10&hl=en&site=&tbm=isch&source=hp&biw=980&bih=710&q=" + YourQueryString
You need the tbm query parameter set to the value isch for image search. So the minimal URL would look like:
"http://google.com/search?tbm=isch&q=" + queryString

Android gallery for private photos

How can I use default Android gallery to display only application's photos? Basically I need to display only my application photos on gallery.
Many Thanks
Sure! You have to parametrize the Intent that is launching the Gallery Application: Set Action to Intent.ACTION_PICK. Set MIME type to one of the image types, or image/* . Set the data to the file to be shown's Uri.
Intent galleryStarter = new Intent();
galleryStarter.setAction(Intent.ACTION_PICK);
galleryStarter.setType("image/*");
galleryStarter.setData(Uri.fromFile(imageFileToShow));
context.startActivity(galleryStarter);

Categories

Resources