Add option to share image from Adapter - android

I was working on a Gallery type app. I wanted to add a share option whenever the specific image is tapped but have failed to do so.
I tried to create Intents, It did not work. What should I do?
Here is my Adapter Class code: https://gist.github.com/mukundmadhav/e63fcf7e34414b9b88458400a6d55651
(Also for testing I have saved 8 images in drawable folder)
When I use Intents it shows this error message:
Error:(51, 17) error: cannot find symbol method startActivity(Intent)
I have imported the android.content.Intent but the error still persists.

try this in adapter :
viewHolder.img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here write code to share image //get clicked image bitmap and then share
Bitmap bitmap = ((BitmapDrawable)viewHolder.img.getDrawable()).getBitmap();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap , "Title", null);
// Uri uri = Uri.parse("android.resource://your package
//name/"+R.drawable.ic_launcher);
Uri uri = Uri.parse(path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing");
startActivity(Intent.createChooser(shareIntent, "Send your image"));
}
});

Related

How to share image of app

I have picture in drawable and I want to share that one image.
Following is my code,
btnic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.defaultpicture);
Intent ppp = new Intent("android.intent.action.SEND");
ppp.setType("image/png");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "defaultpicture", null);
Uri uri = Uri.parse(path);
ppp.putExtras(Intent.EXTRA_STREAM, uri); **i am getting error here**
MainActivity.this.startActivity(Intent.createChooser(ppp, "Send picture"));
Toast.makeText(getApplicationContext(), "Picture Copied", Toast.LENGTH_SHORT).show();
}
});
where am i doing wrong?
Intent.EXTRA_STREAM takes value as Local path of file on storage not on resource (Not a resource ID).
So you need to first save the image as file in storage then pass the url to sharing Intent .
First decode resource.
Bitmap b = BitmapFactory.decodeResource(res, id);
Save it to specific location on storage and use this file path as value in intent. you can save it as below or in some other way.
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "myFile", null);
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "yourimage.jpg");
Log.d("TAG", "onClick: "+photoFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using"));
for more about Sharing Content with Intents

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.

Share Image in Twitter in Android using Intent

Hello I try to use the share dialog using Intent with the code below. I want to share an image and text at the same time. However I get the eror: Failed to insert image java.io.FileNotFoundException: No such file or directory
My code is as below, am I doing anything wrong. The code is in a fragment class.
Bitmap image = bmResized;
String pathOfBmp = Images.Media.insertImage(getActivity().getContentResolver(), image, "twitter_image.jpg", null);
Uri bmpUri = Uri.parse(pathOfBmp);
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.setAction(Intent.ACTION_SEND);
tweetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "here is the tweet text");
tweetIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
tweetIntent.setType("image/jpeg");
startActivity(Intent.createChooser(tweetIntent, "Share this via"));
You can look Fabric library. For twitter process usually using Fabric library.
You can look Fabric document:
https://docs.fabric.io/android/twitter/compose-tweets.html
Also this is github link:
https://github.com/fabric/fabric
Try this:
Drawable mDrawable = mImageView.getDrawable();
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image Description", null);
Uri uri = Uri.parse(path);
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.setType("image/jpeg");
tweetIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(tweetIntent, "Share this via"));

Android : Share drawable resource with other apps

In my activity I have an ImageView. I want ,when user click on it, a dialog opens (like intent dialogs) that show list of apps which can open image than user can choose a app and show the image with that app.
my activity code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView) findViewById(R.id.imageid);
iv.setImageResource(R.drawable.dish);
iv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here is where I want a dialog that I mentioned show
}
});
}// end onCreate()
You can't pass a bitmap to an intent.
From what I see you want to share a drawable from your resources. So first you have to convert the drawable to a bitmap. And then You have to save the bitmap to the external memory as a file and then get a uri for that file using Uri.fromFile(new File(pathToTheSavedPicture)) and pass that uri to the intent like this.
shareDrawable(this, R.drawable.dish, "myfilename");
public void shareDrawable(Context context,int resourceId,String fileName) {
try {
//convert drawable resource to bitmap
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
//save bitmap to app cache folder
File outputFile = new File(context.getCacheDir(), fileName + ".png");
FileOutputStream outPutStream = new FileOutputStream(outputFile);
bitmap.compress(CompressFormat.PNG, 100, outPutStream);
outPutStream.flush();
outPutStream.close();
outputFile.setReadable(true, false);
//share file
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));
shareIntent.setType("image/png");
context.startActivity(shareIntent);
}
catch (Exception e) { Toast.makeText(context, "error", Toast.LENGTH_LONG).show();
}
}
You have to startActivity using intent of type Intent.ACTION_VIEW-
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(<your_image_uri>, "image/*");
startActivity(intent);
Create a chooser by using the following code. You can add it in the part where you say imageview.setonclicklistener().
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

How to use bitmap to send to another app

can someone help me how to send an image to another application in android?
I want to send an image to be used as wallpaper, BBM display picture, and other applications that can use it (like WhatsApp, contacts, etc.)
I use this code, but it can only be used for sending text and not images as I want
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
Then I tried to use this code to set the wallpaper
public void setAsWallpaper(Bitmap bitmap) {
try {
WallpaperManager wm = WallpaperManager.getInstance(_context);
wm.setBitmap(bitmap);
//disinimas
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(_context,
_context.getString(R.string.toast_wallpaper_set_failed),
Toast.LENGTH_SHORT).show();
}
}
the problem with the code, bitmap images directly applied as wallpaper. I wanted like sending text above, the user can choose to use another application. So I want a bitmap image that can later be used for wallpaper, BBM display picture, or other applications that support it
bitmap variable already contains the image that I want to, the images obtained from the internet with this code:
Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
I use this code and its work, but give me a message BBM: File not found, WhatsApp: File is not an image:
Bitmap icon = bitmap;
Intent share = new Intent(Intent.ACTION_ATTACH_DATA);
share.setType("image/jpeg");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
Thanks for your help Antrromet, finally I solved my problem with the following code:
Bitmap icon = bitmap;
// Intent share = new Intent(Intent.ACTION_SEND);
// share.setType("image/*");
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/*");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/*");
intent.putExtra("mimeType", "image/*");
this.startActivity(Intent.createChooser(intent, "Set as:"));
Now the image can be used as Wallpaper, BBM profile picture, WA display Picture, Coontact display picture, etc. Thanks
Did you try using the following code? It is use to send binary data to other apps.
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)));
Or if you dont have the URI with you but have the Bitmap instead, then try using the code as given here.
UPDATE:
Setting wallpaper and profile picture for BBM are totally different things and there is no common intent for them. For setting the wallpaper, you can try the following as given here.
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/jpeg");
intent.putExtra("mimeType", "image/jpeg");
this.startActivity(Intent.createChooser(intent, "Set as:"));
For BBM, the APIs are not open for changing the user image. So you cannot do that through your app.

Categories

Resources