Open remote image in gallery - android

I have an app with thumbnails and when the user touch a thumb, I would like to open the full image in fullscreen. I try opening the image in the default Gallery app, but it doesn't work on all devices.
Here's the snippet :
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url_of_the_remote_image_here));
intent.setType("image/jpg");
startActivity(intent);
On Nexus S running 4.1, I get :
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=image/jpg }
I try several snippets of code and many were opening the image in the browser. Because all Android devices have default gallery, shouldn't it be possible to open the remote image using it?

You should never assume there's activty to handle your intenet, so always wrap startActivity() in try/catch. I'd use correct mime type image/jpeg, but in fact I'd replace it with just more generic image/*.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url_of_the_remote_image_here));
intent.setType("image/*");
try {
startActivity(intent);
} catch( Exception e ) {
e.printStatckTrace();
}

Related

How to open other apps from my webview app like Instagram etc?

I am working on my app which will show my website and i am getting intent error in my Android Webview app whenever I try to open Instagram profile. I want it to be open in my Instagram app can someone help me out by code please
Please help me out Click here to see the error I got
Seems that url you created could be invalid, try to create your intent with urls like these:
Uri uri = Uri.parse("http://instagram.com/_u/xxx");
Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);
likeIng.setPackage("com.instagram.android");
try {
//instagram app exists on device
startActivity(likeIng);
} catch (ActivityNotFoundException e) {
//instagram app does not exist on device, using browser
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://instagram.com/xxx")));
}

Android open image in gallery with slide through images

I am developing Android camera app, and would like to have feature that is in most of camera apps:
Clicking on the preview icon opens the gallery, that shows current photo and allows to swipe through all images in the camera folder.
When I use Intent.ACTION_VIEW, the gallery shows only 1 image, without possibility to look through other photos in the directory.
Are there any extra flags for intent Intent.ACTION_VIEW to get such behavior?
Is there any workaround to get such behavior?
Are there any extra flags for intent Intent.ACTION_VIEW to get such behavior?
No.
There is no requirement that an Android device have an app that is capable of "shows current photo and allows to swipe through all images in the camera folder". For those devices that happen to have such an app, there is no standard Intent structure that demands that the app "shows current photo and allows to swipe through all images in the camera folder".
Is there any workaround to get such behavior?
Write your own UI for this.
#CommonsWare might sound a bit harsh, but if you're going to make an App, don't expect you'd need just some magic Intent calls that do all the work for you.
For the kind of thing you want to achieve, look at ViewPager that gives you something to swipe between different views (each of which would then show one image). Chris Bane has provided a nice custom view that allows to view, zoom and scroll pictures: https://github.com/chrisbanes/PhotoView
OpenCamera AlmalenceGUI.openExternalGallery() uses undocumented action="com.android.camera.action.REVIEW" which in my android-4.2.2 opens the gallery in swipe mode.
private void openExternalGallery(Uri uri)
{
try
{
Intent intent = new Intent("com.android.camera.action.REVIEW", uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
ApplicationScreen.instance.startActivity(intent);
} catch (ActivityNotFoundException ex)
{
try
{
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
ApplicationScreen.instance.startActivity(intent);
} catch (ActivityNotFoundException e)
{
Log.e("AlmalenceGUI", "review image fail. uri=" + uri, e);
}
}
}

How to use startActivity to open data uri

I want to open a data: url containing a pdf in an activity in my android application. I have something like the following code:
String url = "data:application/pdf;base64,JVBERi0xLjIgDQol4uPP0w0KIA..."; // shortened for brevity
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
I am seeing an error:
error opening uri: No Activity found to handle Intent {
act=android.intent.action.VIEW dat=data:application/pdf... (followed
by the rest of the data url).
How can I resolve this?
Since approximately zero apps in existence will support that scheme, you will need to decode the PDF yourself, write it to a file, and then open the PDF viewer on the file.
You need to have an app that can handle opening pdf documents. If you still wish to do this catch an ActivityNotFoundException and tell the user to download one.

ActivityNotFoundException is not triggered to open a downloaded PDF file

I want to download a PDF from a url and also want to trigger catch phrase if no PDF Viewer is detected.
Here's my code:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)));
} catch (ActivityNotFoundException e) {
openDialog(getString(R.string.error),
getString(R.string.no_pdf_reader));
}
Now the problem is that ActivityNotFoundException is never triggered because it always download the PDF even if there is no PDF Viewer around. How do you suggest I do this?
EDIT:
Here's my old code:
Intent pdfIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl));
pdfIntent.setType("application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(pdfIntent);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(materialPdfUrl)));
is starting an Implicit Intent and therefore will not throw ActivityNotFoundException.
If you read this http://developer.android.com/guide/components/intents-filters.html#ccases
Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.
Therefore if no PDF viewers are found the Android Download Manager will attempt to download the file (rather than throw that exception).
If you want to view the pdf or be told you cannot view it (rather than download) then you will need to query the system manually using the PackageManager to find out if an application will respond to your intent rather than just firing and forgetting.
FYI ActivityNotFoundException will be thrown for Explicit Intent's something like:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.facebook","NewsFeedActivity.java"));
startActivity(intent);```
I would recommend using the PackageManager to detect if the system will handle a PDF intent for you.
PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW)
.setType("application/pdf");
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY;
if (list.size() > 0) {
// Happy days a PDF reader exists
startActivity(intent);
} else {
// No PDF reader, ask the user to download one first
// or just open it in their browser like this
intent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("http://docs.google.com/gview?embedded=true&url=" + pdfUrl);
startActivity(intent);
}
See this blog post for more info on checking intents. The added benefit of this approach is that you can grey out/remove menu options before the app even tries to execute the Intent. Then you can explain to the user in a slightly more friendly way that they need to grab a PDF viewer app, or indeed apply some logic to fallback to a web based PDF viewer.
Having trialled this approach with Foxit Reader and Adobe Reader they both seem to have different behaviours. Foxit will not download the PDF for you, it will redirect you to the browser and download the file. Adobe will download the file for you then display it.
So to get round this difference once you have detected that a PDF viewer is available then you will probably want to download the PDF to the SD card, for example the downloads folder. This is probably best achieved in an AsyncTask, or you might be able to use the DownloadManager. Then open the local file Uri in the preferred reader. This should get round the difference in behaviour. Maybe open a ticket with Foxit to bring it into line with Adobe? ;)
The function startActivity() you use is not on the condition that the PDF reader is not exist, it only download the PDF from the URL, and if there are PDF Readers then it will offer a selector, just as the function of clicking on a PDF file.
you may try this code. This may helpful to you.
Intent intent = new Intent();
intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader");
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
try {
startActivity(intent);
} catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(
getApplicationContext());
builder.setTitle("No Application Found");
builder.setMessage("Download application from Android Market?");
builder.setPositiveButton(
"Yes, Please",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
DialogInterface dialog,
int which) {
Intent marketIntent = new Intent(
Intent.ACTION_VIEW);
marketIntent.setData(Uri
.parse("market://details?id=com.adobe.reader"));
mProgressDialog.dismiss();
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks",
null);
builder.create().show();
}
That is because your device or emulator does not have an application capable of viewing a local PDF file.
Whenever you start an intent, you should have the native app installed on the emulator to handle that intent. Ex. If you invoke an intent with Maps as the action, you would have to use the Google API's based emulator. By default, android emulator does not have a PDF reader. You could test this on a device with a PDF reader and it should work fine.
use startActivityForResult(..).
See the link here.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivityForResult(intent, REQUEST_CODE);
You will get the result in onActivityResult(..) of your activity.

Intent for selecting wallpaper with wallpaper region highlighting

I would like to know if it's possible to create an Intent that makes the gallery cropper show wallpaper highlighting. This feature has been introduced in Honeycomb. To get an idea of what I'm looking for have a look at the tablet on the image (the three blue rectangles).
I had a look at the source code of the ICS gallery app, but I couldn't find what I'm looking for.
I would like to know if it's possible to create an Intent that makes
the gallery cropper show wallpaper highlighting.
Assuming you want your app to behave properly across all Android devices, the answer is no. Neither the cropping activity nor the highlighted crop-view is part of the public API; both are internal to the Gallery 3D app. In other words, you could spend all the time in the world trying to find an Intent action to get this to magically work for you, but the fact is that some devices simply won't support it. For example, many devices, such as the HTC Sense and Samsung Galaxy, have customized Android versions that have their own gallery app. Since these Gallery apps are specific to the companies that designed them, these devices won't necessarily have a CropImage class for you to launch.
That being said, in order to guarantee that your application works across all devices, you'll have to incorporate the cropping code directly into your project. And if for some reason you find a way to launch the crop activity with an Intent, you should test to see whether the com.android.gallery3d package exists at the very least, and handle it somehow.
I have included below a work-around that might help you incorporate the Android code into your project. I don't currently have access to a tablet running Honeycomb/ICS so I can't be more specific with regards to how to get it working on newer versions of Android, but I imagine it involves similar analysis and a bit of copying and pasting from the com.android.gallery3d package.
Reusing the "Crop Activity" on Android 2.x
I tested this on my Nexus One and just before the soft "crop-rectangle" popped up, I got the following logcat output:
I/ActivityManager( 94): Starting: Intent {
act=android.intent.action.CHOOSER
cmp=android/com.android.internal.app.ChooserActivity (has extras) } from pid 558
I/ActivityManager( 94): Starting: Intent {
act=android.intent.action.ATTACH_DATA
dat=content://media/external/images/media/648
typ=image/jpeg
flg=0x3000001
cmp=com.google.android.gallery3d/com.cooliris.media.Photographs (has extras) } from pid 558
I/ActivityManager( 94): Starting: Intent {
dat=content://media/external/images/media/648
cmp=com.google.android.gallery3d/com.cooliris.media.CropImage (has extras) } from pid 558
So from what I can tell, the sequence of events that occurs when you perform this action is as follows:
You navigate to an image in the gallery and select "set as...". An ActivityChooser pops up and you select "Wallpaper".
This selection fires an Intent with action ATTACH_DATA and component com.cooliris.media.Photographs, which is a class in the Android framework that serves as a "wallpaper picker" for the camera application; it just redirects to the standard pick action. Since we have given the Intent a URI that specifies the image to set as the wallpaper, this class will inevitably execute the following code (see the class's onResume method):
Intent intent = new Intent();
intent.setClass(this, CropImage.class);
intent.setData(imageToUse);
formatIntent(intent);
startActivityForResult(intent, CROP_DONE);
This fires another Intent that starts the CropImage Activity... this is where you specify the cropped area using the soft-rectangle. When you specify the crop, the result is set to RESULT_OK with requestCode = CROP_DONE. The Photographs Activity switch-cases over these two constants and then sets the wallpaper accordingly (see the Photographs class's onActivityResult method).
Unfortunately, for whatever reason the Android team decided to removed these functionalities from the SDK beginning with API 4 (Android v1.6)... so if you wanted to fire an Intent to perform these exact sequence of events, it would require you to sift through the com.cooliris.media package, and to copy and paste the relevant classes into your project. In my past experience, doing this is often more trouble than it is worth (unless it is to perform a relatively simple action) but it is definitely possible.
Here is a nice tutorial on how you might go about simplifying the process... it requires you to copy and paste 12 Java classes into your project as opposed to the entire com.cooliris.media package. These classes together should be enough to correctly fire up the CropImage Activity, but you will have to set the wallpaper manually upon the CropImage Activity's result.
Also note that the sample code provided assumes that you want to crop immediately after a picture is taken by the camera. In order to, for example, start the CropImage Activity with a pre-selected image from the gallery, you'd call,
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
and then in onActivityResult, (if requestCode == ACTIVITY_SELECT_IMAGE and resultCode == RESULT_OK), launch the CropImage Activity with the Uri data given in the onActivityResult's third argument (see the sample code for an example on how to launch the Activity).
If anything, hopefully this will help point you in the right direction. Let me know how it goes and leave a comment if you want me to clarify anything.
I this will help:
public class CropSelectedImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
final Bundle extras = data.getExtras();
Uri photoUri = data.getData();
if (photoUri != null) {
Intent intent = new Intent("com.android.camera.action.CROP");
//intent.setClassName("com.android.camera", "com.android.camera.CropImage");
intent.setData(photoUri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
}
}
}
}
taken from: ImageCropper
I haven't tried this but if you have a look here
Bundle newExtras = new Bundle();
// maybe that here - for more options see your source code link
newExtras.putString("circleCrop", "true");
Intent cropIntent = new Intent();
// Uri would be something from MediaStore.Images.Media.EXTERNAL_CONTENT_URI
cropIntent.setData(img.fullSizeImageUri());
// edit: it's inside com.android.gallery in case that is even installed.
// should work if you replace that with setClassName("com.android.gallery", "com.android.camera.CropImage")
cropIntent.setClass(this, CropImage.class);
cropIntent.putExtras(newExtras);
startActivityForResult(cropIntent, CROP_MSG);
Then this might work for you.
Via pick intent maybe that way:
Intent i = new Intent(Intent.ACTION_PICK);
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivity(i);
Just Do this!
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA).setDataAndType(contentUri, "image/jpeg")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra("mimeType", "image/jpeg");
startActivity(Intent.createChooser(intent, getString(R.string.set_as)));
which "image/jpeg" is the image mimeType,
contentUri is the image uri
There is a nice library that's based on ICS's cropping screen (from the gallery app) , here .
You could modify it to your needs, to select the part to be cropped.
The code is based on Android's Gallery app (link here), under "/com/android/camera/gallery" , while the most important class is "CropImage" in "/com/android/camera/" . It's available for all even in case the library will be missing (Google's code is always available), as such:
git clone https://android.googlesource.com/platform/packages/apps/Gallery3D/
(and even if this won't be available, I'm sure there will be others)
Advantages over the other solutions here:
independent
customizable
cannot crash due the changes in the ROM. Other solutions assume the existance of exact classes and apps.
open source.
a real implementation, and not starting an intent to another app.
other solutions are highly non-recommended, just because of the usage of non-official intents, as written here . This is written by a very well known StackOverflow user called "CommonsWare" , who is very respectable user that you can count on in a lot of Android-related topics.
Again, the most recommended thing for cropping images is still a third party library. Not using workarounds of intents.
Try this
// Set Home wallpaper the image
public void setHomeWallpaper () {
BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());
Bitmap bitmap = bitmapDrawable.getBitmap();
String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap, "", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA).setDataAndType(bitmapUri, "image/jpeg")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra("mimeType", "image/jpeg");
startActivity(Intent.createChooser(intent, getString(R.string.background)));
Toast.makeText(PicassoDisplayImageAdapter.this, "قم بإختيار التطبيق المناسب لتعيين الصورة", Toast.LENGTH_SHORT).show();
}

Categories

Resources