Android FileOutputStream - android

i am attempting to save an image file using "openFileOutput" and then adding that file to my intent with EXTRA_STREAM. but logcat keeps saying that file size is 0, i have the proper permission in my manifest.
FileOutputStream fos = openFileOutput("p001.jpg", Context.MODE_WORLD_READABLE);
File jpg = getFileStreamPath("p001.jpg");
fos.close();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_SUBJECT, "Fail picture");
share.putExtra(Intent.EXTRA_TEXT, "Epic fail!!!");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(jpg));
startActivity(Intent.createChooser(share, "Choose share method."));
updated code
private void HandleSMS() throws IOException {
FileOutputStream fos = openFileOutput(R.drawable.p001, Context.MODE_WORLD_READABLE);
File jpg = getFileStreamPath("p001.jpg");
fos.close();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_SUBJECT, "Fail picture");
share.putExtra(Intent.EXTRA_TEXT, "Epic fail!!!");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(jpg));
startActivity(Intent.createChooser(share, "Choose share method."));
}

So, do one thing instead of the direct name use R.drawable.NameOfTheImage.

Related

Android Share Intent Image Sharing not working

I am new to Android and is having problem sharing image through share intent. I have googled alot, tried various things but still could not find the solution.
My code is:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
this.startActivityForResult(Intent.createChooser(shareIntent,"Share with"),12);
I have checked the uri, the file saved is a bitmap and its returning file. But the image which shows during sharing is not valid. Gmail says it could not attach the attachment, messages app could not load the image.
Text sharing is working fine.
Basically I am writing a plugin for Unity. Here is my code on Unity Side:
string destination = Path.Combine(Application.persistentDataPath,"sharingImage" + ".png");
if (!File.Exists(destination)) {
print("creating new file");
File.Create(destination);
}
File.WriteAllBytes(destination, bytes);
print("destination= "+destination);
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://" + destination);
using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.kashiftasneem.sharelib.ShareController")) {
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
pluginClass.CallStatic("Share",message,uriObject,jo,gameObject.name,destination);
}
I am logging destination and uri, they are:
destination=
/data/data/com.kashiftasneem.shareandroidplugin2/files/sharingImage.png
uri =
file:///data/data/com.kashiftasneem.shareandroidplugin2/files/sharingImage.png
Try this code:
Bitmap bitmap = getBitmapFromView(idForSaveView);
try {
File file = new File(this.getExternalCacheDir(), "appdiam.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
//fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
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)));
It might help.

Send Image as email attachement in android

I am getting an error that i cannot attach empty file in gmail.I am trying to build simple app in which when i click on the button it show choices by which i can send image,But the below code is not working.
Please help i am novice in android.
code:
if(view.getId()==R.id.SendImage)
{
Uri imageUri = Uri.parse("android:resource://com.example.jaspreet.intentstest.drawable/"+R.drawable.image);
intent=new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_STREAM,imageUri);
intent.putExtra(Intent.EXTRA_TEXT,"Hey i have attached this image");
chooser=Intent.createChooser(intent,"Send Image");
startActivity(chooser);
}
Try using this:
shareIntent.setType("image/png");
By using this the intent know that it will send .png file.
On this link you can find a list of all media type/subtypes
Try this
Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
Try this snippet
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);
startActivity(Intent.createChooser(share, "Select"));
An android:resource:// is not a File, and probably you are messing up your Uri by converting to a File and then back to a Uri.

Android Application with Intents

I'm trying to do an Intent to share an image from an SD card. But I have a problem with the share button: It says:
no app can perform this action
So, which type of code I have to use to share an image? I want, maybe, to have the possibility to send my image as an e-mail or a message. Hope someone can help me.
first you need to store img in storege
like this way
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
and then you can share it like this way
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
"no app can perform this action" because, there was no sharing application installed in your device. Install some third party applications like facebook, whats app, gmail... in your real device and run your application to share image.
and also start activity like below:
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

How to share image and text both in facebook?

I can share image and text in Facebook messenger but not in Facebook. How to do that? I am using the following code.
Intent sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Body text of the new status");
context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
Facebook does not currently support:
EXTRA_TEXT besides URLs
EXTRA_STREAM from external sources (only local files)
Until Facebook supports those features, you are limited to only local images without any text.
Try out as below :
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/OnItsOwn/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "sample.jpg");
FileOutputStream fOut = new FileOutputStream(file);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// Share
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "Body text of the new status");
startActivity(Intent.createChooser(share, "Share Image"));

photo share intent in android

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");
Uri uri = Uri.parse(pathToImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
return shareIntent;
i was used above code for sharing image on social sites.when i posting image on facebook only text is posted and image is not coming.how can we get the image and pathtoimage is string variable i am getting sever image path and stored in string variable.but it is not working.please give any solutions for my above question.?
Try this
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
Refer more links
http://android-developers.blogspot.in/2012/02/share-with-intents.html
Android Share Via Dialog
Image post into facebook wall from android sdcard
This is the solution I came up with:
This function allows you to share to instagram, facebook or any other app if you know the package name. If the app is not installed it redirects to the Play Store. If you send "others" as parameters, you will display a list of apps that also support image sharing.
shareImageIntent(mActivity,"image/*", "/storage/emulated/0/Pictures/Instagram/IMG_20150120_095603.jpg", "Instagram Sharing test. #Josh","com.instagram.android"); // parameters - for facebook: "com.facebook.katana" , to display list of other apps you can share to: "others"
public void shareImageIntent(Activity a,String type, String mediaPath, String caption, String app){
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(app);
if ((intent != null)||(app.equals("others"))){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
if(!app.equals("others"))
share.setPackage(app);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI and the caption to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_TEXT, caption);
// Broadcast the Intent.
//startActivity(Intent.createChooser(share, "Share to"));
a.startActivity(share);
}
else{
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intent.setData(Uri.parse("market://details?id="+"com.instagram.android"));
intent.setData(Uri.parse("market://details?id="+app));
a.startActivity(intent);
}
}
You can not use Intent to post to Facebook. They have specifically blocked this. You have to either use their SDK or copy it to the clipboard and have the user paste it after the facebook interface opens. I am having this same problem.

Categories

Resources