Sharing images fails to load through intent - java

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)

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);
....

Converting MediaRecorder .3gpp to Byte and then retrieve back to .3gp not working

I am having a very weird issue with converting a 3gpp(origin) file to bytes and retrieving bytes back to 3gpp(2nd) file.
The 2nd 3gpp file is I can't play using MediaPlayer and neither can play in my computer when transferred into. Also, 2nd 3gpp file size is little less than origin 3gpp.
1. This is how I initialize MediaRecorder
String voiceStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + File.separator + "media" + File.separator + "voices" + File.separator + "temp.3gpp";
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(voiceStoragePath);
2. After Recording finished this is how I convert it to a byte array
File file = new File(voiceStoragePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
for (int readNum; (readNum = fis.read(b)) != -1;) {
bos.write(b, 0, readNum);
}
byte[] bytes = bos.toByteArray();
3. Now saving bytes into .3gpp file
String voiceStoragePath2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getPackageName() + File.separator + "media" + File.separator + "voices" + File.separator + "temp2.3gpp";
File path = new File(voiceStoragePath2);
FileOutputStream fos = new FileOutputStream(path);
fos.write(bytes);
fos.close();
4. Device File Explorer
5. Playing it using MediaPlayer
mediaPlayer.setDataSource(outputFile);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(this);
Log:E/MediaPlayerNative: error (1, -2147483648)
Well, that happened. It was so obvious that I have to stop MediaRecorder first before I convert 3gpp into a byte array. Still can't believe how I missed that one.
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
byte[] bytes = convert(voiceStoragePath);

Camera intent does not work after clicking more than 10 images?

I am clicking images from android using camera intent.It opens the native camera app of android phone.I call onActivityResult to get the data from camera app.When i try to click more than 10 images continuously then on click of 'ok' button in camera it's not navigating me back to my app but on cancel it does.please tell me how can i get the solution?
Code
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imagesFolder = new File(Environment.getExternalStorageDirectory(),
"DW");
imagesFolder.mkdirs();
// Create new folder
String timeStamp = new SimpleDateFormat("MM/dd/yyyy_hhmmssa")
.format(new Date());
// if (component_id != comp_id) { image_paths.clear(); }
component_id = comp_id;
String alter_timeStamp = timeStamp.replaceAll("/", "_");
cam_client_id = client_id;
cam_sub_client_id = sub_client_id;
fname = comp_name + "_" + component_id + "_" + alter_timeStamp
+ ".jpg";
fname = fname.replaceAll(",", "");
fname = fname.replaceAll("&", "");
fname = fname.replaceAll(" ", "_");
Log.i("SYNC", "fname is ->" + fname);
temp_image = image = new File(imagesFolder, fname);
// compress image size
temp_uri = uri = Uri.fromFile(image);
file_uri = uri.getPath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
context.startActivityForResult(intent, 90);
Thanks

Want to show just captured photo instead of live view

When I capture a photo from camera, I want to show it instead of live view.
Is there anything wrong with the URI or should I write a return page code?
What I am doing so far:
public void btnTakePhotoClicked(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String fullPath = "";
File imageFile = null;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String pictureName = "IMG_" + timeStamp + ".jpg";
fullPath = Environment.getExternalStorageDirectory() + "/ImageFolder/" + pictureName;
File dir = new File(Environment.getExternalStorageDirectory() + "/ImageFolder/");
imageFile = new File(fullPath);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

Android: Share Bitmap Not Saved To SD

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;

Categories