How to attach image from drawable to gmail? - android

I am trying to attach image from my gridview to gmail or facebook,but whenever i tried to attach my app got crash,and i am getting following error with nullpointer exception,following is my code with gridview image selection,can any one help?
public class Free_Cover_Activity extends AppCompatActivity
{
GridView grid;
int[] imageId = {
R.drawable.discovercover,
R.drawable.burpresswordfree,
R.drawable.flyfree,
R.drawable.cantmovefree,
R.drawable.cantmovewordfree,
R.drawable.chalkthisfree,
R.drawable.fivehundredmetersfree,
R.drawable.freeexercise,
R.drawable.gym_smilie,
R.drawable.hundredcalrairesfree,
R.drawable.injuryfree,
R.drawable.jumpropefree,
R.drawable.nicesnathcfree,
R.drawable.personglrecordfree,
R.drawable.posefree,
R.drawable.pushupfree,
R.drawable.shoulder,
R.drawable.timewordfree,
R.drawable.unbrokernfree,
R.drawable.weightbeltfree
};
private Bitmap mBitmap;
private Intent email;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.free_cover_gridview);
android.support.v7.app.ActionBar abar = getSupportActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#D64365"));
abar.setBackgroundDrawable(colorDrawable);
abar.hide();
CustomGridFreeCover adapter =
new CustomGridFreeCover(Free_Cover_Activity.this, imageId);
grid=(GridView)findViewById(R.id.freecover_grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
try
{
Bitmap largeIcon =
BitmapFactory.decodeResource(getResources(), R.drawable.discovercover);
/*
replace "R.drawable.bubble_green" with the image resource
you want to share from drawable
*/
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f =
new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
//remember close de FileOutput
fo.close();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
//set your subject
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi");
//set your message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you");
String imagePath = Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
});
}
public class CustomGridFreeCover extends BaseAdapter
{
private Context mContext;
//private final String[] web;
private final int[] Imageid;
public CustomGridFreeCover(Context c,int[] Imageid )
{
mContext = c;
this.Imageid = Imageid;
//this.web = web;
}
#Override
public int getCount()
{
//TODO Auto-generated method stub
return Imageid.length;
}
#Override
public Object getItem(int position) {
//TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
//TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
//TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
grid = new View(mContext);
grid = inflater.inflate(R.layout.free_cover_griditem, null);
//TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image_freecover);
//textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
}

Here is the working code which you need:
Firstly save image from Drawable to SD Card here is the code:
try{
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.bubble_green);
//replace "R.drawable.bubble_green" with the image resource you want to share from drawable
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
// you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
f.createNewFile();
// write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Then get the saved image from SD card and attach in the email intent like this:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); //set your subject
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); //set your message
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Image"));

This is what worked for me perfectly.
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.share_message));
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.image_to_share);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
But I will always recommend to decode a Bitmap from this resource, save it as a File in the storage and then use that file. A more reliable way this directly using from the resources.

roman thing is you are passing a null bitmap object
Bitmap icon = mBitmap;
you need to assign bitmap first then the code will start work
for more info you can have a look on to below links :-
https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents
http://developer.android.com/training/sharing/send.html

First Convert your drwable to image and store it on SDCard
Store Drawable to SD card
Then
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named yourImg.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/yourImg.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}

Related

share listview item (url image and text ) on facebook

I am fetching data from server and set it into listview. Each Listview item have photo ( url image ) and a text + share button . I have implemented all the code and working perfect.. But can any one help me.. How to implement facebook share intent when click on button of particular listview item.I want to share image and text
i ask from a way to the this
and thank you in advance
this is my code "MediaAdapter.java" :
public class MediaAdapter extends ArrayAdapter<Media> {
ArrayList<Media> mediaList;
Context context;
int Resource;
LayoutInflater vi;
ViewHolder holder;
ImageLoader imageLoader;
public MediaAdapter(Context context, int resource, ArrayList<Media> objects) {
super(context, resource, objects);
imageLoader = new ImageLoader(context);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
mediaList = objects;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
v = vi.inflate(Resource, null);
holder = new ViewHolder();
int loader = R.drawable.ic_launcher;
//l url d image
holder.imageview = (ImageView) v.findViewById(R.id.urlImage);
// load image
imageLoader.DisplayImage(mediaList.get(position).getUrl(), loader, holder.imageview );
holder.titre = (TextView) v.findViewById(R.id.titre);
holder.titre.setText(mediaList.get(position).getTitre());
v.setTag(holder);
holder.button = (Button) v.findViewById(R.id.btnOne);
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(v.getContext(), "test",Toast.LENGTH_SHORT).show();
}
});
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView titre;
public Button button;
}
}
Change this in the code ....
First remove this line from the constructor
imageLoader = new ImageLoader(context);
Change it to
ImageLoader imageLoader = ImageLoader.getInstance();
Secondly remove this line from the code :
imageLoader.DisplayImage(mediaList.get(position).getUrl(), loader, holder.imageview );
Change it to
imageLoader.loadImage(mediaList.get(position).getUrl(), new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,mediaList.get(position).getTitre());
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(context,loadedImage));
try {
context.startActivity(shareIntent);
} catch (Exception ex) {
Toast.makeText(context, ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
});
Use this method to transform bitmap to uri:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),inImage, "Title", null);
return Uri.parse(path);
}
This will open the the share option for sharing to all the other apps including facebook. If facebook is selected by the user,then the image will be opened in the facebook app (if installed).Let me know if it working for you or not.
this is my new coode
it work like a charm
but i have a problem : but i have a problem when i click on share button of the item Number 2 ===== the content of the item number 3 is shared and note the Number 2 and vice-versa ????????????????
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri pictureUri = getLocalBitmapUri(holder.imageview);
if (pictureUri != null) {
// Construct a ShareIntent with link to image
String text = "image : "+mediaList.get(position).getTitre();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(shareIntent);
} else {
// ...sharing failed, handle error
}
///////////////////////////////////////////////////////////
private Uri getLocalBitmapUri(ImageView imageview) {
// TODO Auto-generated method stub
// Extract Bitmap from ImageView drawable
Drawable drawable =holder.imageview.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) holder.imageview.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}

How to set profile photo option in android?

I am using a picture to display from web service.Now how to use image to set as profile picture of whatsapp or any other profile picture option. I am able to save and share an image. But how to provide an option in menu or as button to set picture as->
Similar to this which is used in Gallery..
I used for save and share button for image but don't know how to implement set profile photo.
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
// Save this bitmap to a file.
File cache = activity.getExternalCacheDir();
File sharefile = new File(cache, "save.png"); //give your name and save it.
try {
FileOutputStream out = new FileOutputStream(sharefile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (IOException e) {
}
// Now send it out to share
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + sharefile));
try {
activity.startActivity(Intent.createChooser(share, "Share photo"));
} catch (Exception e) {
}
}
});
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
image.setDrawingCacheEnabled(true);
Bitmap bitmap = image.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/Nokia");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = "Photo-"+ n +".jpg";
File file = new File (newDir, fotoname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Toast.makeText(activity, "Saved to your folder"+fotoname, Toast.LENGTH_SHORT ).show();
} catch (Exception e) {
}
}
});
On button Click :
OnButtonClick(){
ImageProcessing imageProcessing = new ImageProcessing();
Bitmap bitmap = imageProcessing.takeScreenshot(getWindow().getDecorView().findViewById(R.id.view_thought));
imageProcessing.saveBitmap(bitmap);
Intent intent = imageProcessing.setAsOption(this,imageProcessing.getSavedImagePath());
startActivityForResult(Intent.createChooser(intent, "Set image as"), 200);
}
Implement a new class ImageProcessing
public class ImageProcessing {
private File imagesPath;
public void saveBitmap(Bitmap bitmap) {
imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagesPath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("POS", e.getMessage(), e);
} catch (IOException e) {
Log.e("POS", e.getMessage(), e);
}
}
public File getSavedImagePath(){
imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");
return imagesPath;
}
public Bitmap takeScreenshot(View rootView) {
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public Intent setAsOption(Context cntxt,File imagesPath){
/*File imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");*/
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
if(imagesPath.exists()){
Uri contentUri = FileProvider.getUriForFile(cntxt, BuildConfig.APPLICATION_ID+".Utility.GenericFileProvider",imagesPath);
intent.setDataAndType(contentUri, "image/jpg");
intent.putExtra("mimeType", "image/jpg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else {
Toast.makeText(cntxt,"Not a wallpaper",Toast.LENGTH_SHORT).show();
}
return intent;
}
}
In menifest add :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(Uri.parse("file:///" + yourFile), "image/jpg");
intent.putExtra("mimeType", "image/jpg");
startActivityForResult(Intent.createChooser(intent, "Set As"), 200);

save camera image into directory

I am working on a application in which I have to click the image from the camera and save it into directory.I am able to create directory named MyPersonalFolder and also images are going into it but when I am trying to open that image to see, it doesn't open and shows the message that that image cannot be opened. here is my code. Can anyone please tell me what mistake I am doing here.
I have also mentioned permissions in manifest .
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
public class Camera extends Activity{
private static final String TAG = "Camera";
private static final int CAMERA_PIC_REQUEST = 1111;
Button click , share;
ImageView image;
String to_send;
String filename;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
image = (ImageView)findViewById(R.id.image);
share = (Button)findViewById(R.id.share);
click = (Button)findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
// Save this bitmap to a file.
File cache = getApplicationContext().getExternalCacheDir();
File sharefile = new File(cache, "toshare.png");
try {
FileOutputStream out = new FileOutputStream(sharefile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (IOException e) {
}
// Now send it out to share
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + sharefile.getAbsolutePath()));
try {
startActivity(Intent.createChooser(share, "Share photo"));
} catch (Exception e) {
}
/*Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
//String to_send = null;
share.putExtra(Intent.EXTRA_TEXT, to_send);
startActivity(Intent.createChooser(share, "Share using..."));*/
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
FileOutputStream outStream = null;
if (requestCode == CAMERA_PIC_REQUEST) {
//2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
image.setImageBitmap(thumbnail);
//3
share.setVisibility(0);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//4
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/MyPersonalFolder");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
//outStream.write(data[0]);
outStream.flush();
outStream.close();
//Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
refreshGallery(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
/*try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();*/
}
}
}
private void refreshGallery(File file) {
Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(file));
sendBroadcast(mediaScanIntent);
}
}
You will need to use MediaScanner to notify the system of the new file/directory. You can try something like this after creating and saving the new file:
/**
* Adds the new photo/video to the device gallery, else it will remain only visible via sd card
*
* #param path
*/
public static void addToGallery(Context context, String path) {
MediaScanner scanner = new MediaScanner(path, null);
MediaScannerConnection connection = new MediaScannerConnection(context, scanner);
scanner.connection = connection;
connection.connect();
}
/**
* Scans the sd card for new videos/images and adds them to the gallery
*/
private static final class MediaScanner implements MediaScannerConnection.MediaScannerConnectionClient {
private final String path;
private final String mimeType;
MediaScannerConnection connection;
public MediaScanner(String path, String mimeType) {
this.path = path;
this.mimeType = mimeType;
}
#Override
public void onMediaScannerConnected() {
connection.scanFile(path, mimeType);
}
#Override
public void onScanCompleted(String path, Uri uri) {
connection.disconnect();
}
}
EDIT:
You are also forgetting to write the byte array to the file specified in the output stream, like in the code that you have commented out. Try this at the end just before you refresh the gallery:
outStream = new FileOutputStream(outFile);
outStream.write(bytes.toByteArray()); //this is the line you had missing
outStream.flush();
outStream.close();
Also take note that using Intent.ACTION_MEDIA_SCANNER_SCAN_FILE to refresh the gallery can also present you with some security issues on kitkat (cant remember exactly what the issues were). So just make sure you test it on kitkat device to confirm that it works correctly

Why is my share function only working with whatsapp?

I have my full screen activity:
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
BitmapDrawable bm = (BitmapDrawable) imageView.getDrawable();
Bitmap mysharebmp = bm.getBitmap();
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mysharebmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//you can create a new file name "test.jpeg"
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + "test.jpeg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
Log.d("done","done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "test.jpeg";
Uri m = Uri.parse(path);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, m);
startActivity(Intent.createChooser(sharingIntent,
"Share image using"));
}
}
Why is whatsapp the only app what is working?
All other apps are telling that they are failed or unable to download....
I would like to share with every app and i don´t know what i´m doing wrong plz help me i bet it´s easy for you.
Thx dudes!
Closed
Found the solution
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/Pictures/tmp.jpeg"));

Android Carry Camera Image To New Activity

So far I have my app taking a picture AndroidCamera.java using the camera and then saving the image and displaying it in a new Activity Punch.java which works fine. On this screen there are then two options too use the image or retake the image if the button retake is clicked it will go back to the AndroidCamera.java Activity and if use is clicked it will then go to the Activity BeatEmUp.java which is the new Activity I want to show the image on again.
I just cant figure out what to put in the BeatEmUp.java Activity to display the image again in this new Activity you can see on the code below that I am passing the string from AndroidCamera.java to Punch.java but don't think I can do this again from the Punch.java to BeatEmUp.java?
Update Adil Soomro
BeatEmUp.java Activity now force closes when Use button is clicked.
Ok code below has been updated I had to change intent.putExtra("filepath",imagePath);
to Use.putExtra("filepath",imagePath); as with intent at the start it was giving me an error I have also added the BeatEmUp.java as I am not sure if this is correct i thought it would just be the same code as I use to show the image on Punch.java
AndroidCamera.java
PictureCallback myPictureCallback_JPG = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
/*Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
int imageNum = 0;
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
imagesFolder.mkdirs(); // <----
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
OutputStream imageFileOS;
try {
imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();
Toast.makeText(AndroidCamera.this,
"Image saved",
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(getBaseContext(), Punch.class);
intent.putExtra("filepath",Uri.parse(output.getAbsolutePath()).toString());
//just using a request code of zero
int request=0;
startActivityForResult(intent,request);
}};
Punch.java
String imagePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.punch);
imagePath = this.getIntent().getStringExtra("filepath");
Button buse = (Button) findViewById(R.id.buse);
buse.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Use = new Intent(Punch.this, BeatEmUp.class);
Use.putExtra("filepath",imagePath);
startActivity(Use);
}
});
Button bretake = (Button) findViewById(R.id.bretake);
bretake.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent Retake = new Intent(Punch.this, AndroidCamera.class);
startActivity(Retake);
}
});
String myRef = this.getIntent().getStringExtra("filepath");
File imgFile = new File(myRef);
Log.e(">>>", myRef);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imagepunch);
myImage.setImageBitmap(myBitmap);
}
}
}
BeatEmUp.java
String myRef = this.getIntent().getStringExtra("filepath");
File imgFile = new File(myRef);
Log.e(">>>", myRef);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imagepunch);
myImage.setImageBitmap(myBitmap);
Yes. You can pass that image URI again to next Activity.
You just need to store image path in class level variable in Punch.java class, and when starting BeatEmUp Activity, put that Image path again in the Intent and get it in BeatEmUp
Edit:
Take a class level String in Punch.java
String imagePath;
and inside onCreate()
imagePath = this.getIntent().getStringExtra("filepath");
and when starting BeatEmUp Activity
Intent Use = new Intent(Punch.this, BeatEmUp.class);
intent.putExtra("filepath",imagePath);
startActivity(Use);

Categories

Resources