Android: Share Bitmap Not Saved To SD - java

I have an app that takes a screenshot and the shares this with the share intent. Rather than save multiple images each time a user wants to simply share the image. Below is the code I used for images saved to the SD
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
dir = "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Folder/" + location;
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(dir));
startActivity(Intent.createChooser(share, "Share Image"));
However I cant figure out how to simply save a Bitmap such as...
share.putExtra(Intent.EXTRA_STREAM, theBitmap);
Is there a way to do this without saving the image?
Thanks

What I eneded up doing was saving one "Temp" image to the SD/Phone and just overwrite the each time.

Try using this one:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.parse("android.resource://com.your.app/drawable/" + yourimage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Send your image"));
This code works fine for me to send drawables that I use in my app. No pictures saved on sd card. The only problem is that it doesn't work with some social apps :/

Yes you can send the image without having to save it as a file. From just looking at the code you posted I have no idea how your sending the image but you convert the file with the following:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] data;
if(yourBitmap!=null){
yourBitmap.compress(CompressFormat.PNG, 75, bos);
}else if (yourBitmap==null){
Log.w("Image going to server is null","Lost in memory");
}
try{
data = bos.toByteArray();
I dont know if your sending the image to another user via your app or what but thats how I use it upload to a server. When your done with the image you would just nullify it all like this.
bos = null;
data = null;
yourBitmap = null;

Related

Internal storage path

I have this Code to store a Picture:
imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fname_" + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
it stores the Picture in
/file:/storage/emulated/0/fname_.jpg
Then I would like to Show the Picture in imageView
Log.d("URI",imageUri.toString());
File imgFile = new File(imageUri.toString());
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
But It says:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/fname_.jpg (No such file or directory)
I can See the Picture in Interer Speicher/fname_.jpg
Can soebody tell me the real path?
I suspect that imageUri.toString() does not return the path of your file in the SD Card,
You could try to change your Code like this :
File imageFile = new
File(Environment.getExternalStorageDirectory(),"fname_"
+ ".jpg");
mSavedPath = imageFile.getAbsolutePath();
imageUri = Uri.fromFile(imageFile);
intent.putExtra(
android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Then later :
File imgFile = new File(mSavedPath);
....

Sharing images fails to load through intent

I have tried sharing the image to facebook, whatsapp, yahoo but none of them were able to load the image / attach image.
I have the following code :
File pictureFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Intent imageIntent = new Intent(
imageIntent.setAction(Intent.ACTION_SEND);
imageIntent.setType("image/*");
imageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pictureFile));
PendingIntent shareIntent = PendingIntent.getActivity(this, 0, imageIntent, 0);
I have tried using Uri.parse(pictureFile.getAbsolutePath()) and Uri.parse(pictureFile.getParent()) and also tried putting the bitmap instead, but none of these works. Please help me
You need to use FileProvider to generate the Uri for file -
FileProvider.getUriForFile(context, packageName + ".fileprovider", file)

Sending Image through intent after CameraAPI opens

I am trying to build a function for the android phone that will open the CameraAPI and then send the captured picture to a new activity.
In this activity i need it to show a preview for the image and then save.
How could i go about doing this without having to save the image to the SD card?
You can store the image into a bitmap and then convert it to byte array and then transfer to other activity through this code below
Bitmap bitmapPicture= "PICTURE FROM CAMERA";
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
Then Pass Through Intent
Intent imagepass = new Intent(Cam.this,Preview.class);
imagepass.putExtra("imagepass", bytes.toByteArray());
startActivity(imagepass);
In Other Activity we have to recieve the byte array and convert to bitmap and display in imageview through
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("imagepass");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView iv=(ImageView) findViewById(R.id.imgvw2);
iv.setImageBitmap(bmp);
You will have to use custom camera control Hope This Helps.

Posting text and picture using Facebook Android SDK

I am trying to post a picture and text to some user's news feed using the Facebook Android SDK (not using the Dialog), but for some reason it posts the picture and text only to the user's own timeline and not to the global news feed where his friends can see it. If I remove the picture param the text is shown in the news feed. Am I missing something?
This is my code:
//Prepare picture data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
//Post data
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, LoginActivity.mFacebook.getAccessToken());
params.putByteArray("picture", data);
params.putString("message", txt.getText().toString());
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(LoginActivity.mFacebook);
mAsyncRunner.request("me/feed ", params, "POST", new FacebookUploadListener(), null);
mAsyncRunner.request(response, new FacebookUploadListener());
Can I achieve that without the Facebook SDK's dialog?

How to get the size of an drawable image in android

currently i m working in drawable images in android, and now i want to get the size the drawable image in KB , how can i get it, any ideas are welcome.
Here i m getting the default application icons and storing in an drawable array, is creating a new file for each icon for getting length is a good idea? Please suggest any better idea,thanks
Here's an idea. Convert the drawable image to a byte array... the array length is the size of the drawable image in bytes. Not sure if there is a better way...
I think u have to make it a file. But not sure whether it works
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temp" + File.separator);
root.mkdirs();
File file= new File(root, "temp.png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);//bmp is bitmap of ur image
fOut.flush();
fOut.close();
Using file.length() should give you the file size.
I don't know if this will work in all situations but you can use this:
Drawable d;
long kbsize = ((BitmapDrawable)d).getBitmap().getByteCount() / 1024;

Categories