It is necessary to start intent with an image resource.
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.ic_launcher);
Intent intent = new Intent(Intent.ACTION_VIEW, null);
intent.setData(path);
intent.setType("image/png");
startActivity(intent);
There is an error: "the gallery is closed"
Where is my problem?
Related
I'm trying to make a button for sharing an audio file.I get the file format not supported error.I tried all types but always gave the same error(audio/mpeg,audio/aac,audio/wav,audio/ogg,audio/midi,audio/x-ms-wma)
here my codes
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
File f = new File("ses.mp3");
Uri uri = Uri.parse("file://" + f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("audio/mp3");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
mactivity.startActivity(Intent.createChooser(share, "Share audio File"));
Toast.makeText(getApplicationContext(), "Song Shared Successfully", Toast.LENGTH_SHORT).show();
}
});
I am not sure but try to use this Intent
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("*/*");
File imageFileToShare = new File(f.getAbsolutePath());
Uri uri= FileProvider.getUriForFile
(mactivity,mactivity.getPackageName()+".provider",imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
mactivity.startActivity(Intent.createChooser(share, "Share audio File"));
Bundle intent = getIntent().getExtras();`
cardView=(CardView)findViewById(R.id.card);
final String query = intent.getString("Query1");
db = new DataBaseHelper(Image.this);
Cursor c = db.getData(query);
if (c.getCount() != 0) {
c.moveToFirst();
do {
image = c.getString(5);
title=c.getString(3);
} while (c.moveToNext());
}
txt.setText(title);
img.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(image, "drawable", getPackageName())));
To create an Share Intent for image, you can use following code.
uriToImage is Uri of your image file.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
EDIT - To get URI of drawable image
Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
Use Content Provider(FileProvider) To Share Data as it is Good Practice.
Intent intent = new Intent(Intent.ACTION_SEND);
final String path = "Your File Path";
Uri uri = FileProvider.getUriForFile(YourActivity.this, "com.abc.xy.appname.fileprovider", new File(path));
intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
For Multiple File Sharing
ArrayList<Uri> files = new ArrayList<Uri>();
for (int i = 0; i < mfileList.size(); i++) {
File file = new File(mfileList.get(i).get_path());
Uri uri = FileProvider.getUriForFile(YourActivity.this, "com.abc.xy.appname.fileprovider", file);
files.add(uri);
}
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
To Share an Image Stored in Your Drawable.Put this Uri in your Share Intent Extras.
Uri uri = Uri.parse("android.resource://your.package/drawable/image_name");
Check this Link out For Content Provider Setup.
[https://developer.android.com/reference/android/support/v4/content/FileProvider][1]
im getting the following error in my logcat.
: android.os.FileUriExposedException: file:///storage/emulated/0/download/AllCast.apk exposed beyond app through Intent.getData()
it only happening on some devices ie marsh mellow and nougat.
this is where error occurs
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
ive tried adding this line of code
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
also tried this in my app class
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
can anybody tell me what im doing wrong. thanks
EDIT****
Ok guys im trying to get #CommonsWare answer to work i can get the file to download fin to the provider folder. but then my app crashes when it trys to handle the intent. here is my new log cat
05-07 16:25:35.784 8715-8715/com.example.harrops.h20droidapp2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: mypackagename, PID: 8715
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getName()' on a null object reference
at my package name.Casting_Apps$DownloadFileFromURL.onPostExecute(Casting_Apps.java:273)
which leads me to section of code
File newFile = null;
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = newFile.getName().substring(newFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(getBaseContext(), "my package name", newFile);
intent.setDataAndType(contentUri, type);
} else {
intent.setDataAndType(Uri.fromFile(newFile), type);
}
startActivityForResult(intent, ACTIVITY_VIEW_ATTACHMENT);
} catch (ActivityNotFoundException anfe) {
Toast.makeText(getBaseContext(), "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
}
This is the answer i went for.. thanks #CommonsWare for the help
File toInstall = new File(appDirectory, appName + ".apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent)
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
I am new to android i have read mant aricles on this from stackoverflow and other resources but i tried my best to get result
At time of sending i select gmail to send my image but its showing empty file cant be attached
i tried with different images in my package but i get the same response
Here is my code which i tried
Intent intent;
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + getResources().getResourcePackageName(R.drawable.cursor)
+ '/' + getResources().getResourceTypeName(R.drawable.cursor) + '/' +
getResources().getResourceEntryName(R.drawable.cursor) );
intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
chooser=Intent.createChooser(intent, "Select app to send");
startActivity(chooser);
Hope this helps:
BitmapFactory.Options options = new BitmapFactory.Options();
options.outMimeType = "image/jpeg";
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher, options);
String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Image"));
I have a video in my /res/raw/ folder and I would like to start it using the native video player of the device.
This is my code:
String packageName = this.getPackageName();
Uri uri = Uri.parse("android.resource://" + packageName + "/raw/" + R.raw.my_video);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);
When I trie to start the video it crashes and I get this error in LogCat:
09-16 15:05:51.959: E/AndroidRuntime(23298): FATAL EXCEPTION: main
09-16 15:05:51.959: E/AndroidRuntime(23298): java.lang.IllegalStateException: Could not execute method of the activity
I tried Uri.parse(String) on a hosted mp4-file from a random webpage with the same result.
What is my problem?
Issue is you are using wrong raw folder name.
Uri uri = Uri.parse("android.resource://" + packageName + "/raw/" + R.raw.my_video);
Remove and use like this.
Uri uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.my_video);
Hi please try below code hope it will help you
Uri intentUri = Uri.parse("android.resource://"+ packageName + "/" + R.raw.my_video);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(intentUri, "video/mp4");
startActivity(intent);
you add /raw extra in your URI path , Please make it
Uri uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.my_video);