Internal storage path - java

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

Related

How to correctly set the Uri of an image from a folder selected by the user to show in an ImageView?

I need to display an image which I know the file name but I don't know the folder, which must be specified by the user.
I use Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); to get the folder from the user. It returns an Uri with a path along these lines:
content://com.android.externalstorage.documents/tree/primary%3ADownload
Now I need to display an image from this folder in an ImageView. I tried the following:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(chosenFolder + "/image.png"));
ImageView imageView = findViewById(R.id.imageview);
imageView.setImageBitmap(bitmap);
The first line throws the following exception:
java.lang.IllegalArgumentException: Invalid URI: content://com.android.externalstorage.documents/tree/primary%3ADownload/image.png
Replacing %3A by : doesn't work.
How to correctly set the Uri to display the image?
i think you should use file path and not URI . A content:// Uri does not have to represent a file on the filesystem .
try this :
String filePath = null;
if (chosenFolder != null && "content".equals(chosenFolder.getScheme())) {
Cursor cursor = this.getContentResolver().query(chosenFolder, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
} else {
filePath = chosenFolder.getPath();
}
and then create bitmap with file path :
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),
Uri.parse(filepath+ "/image.png"));
I managed to achieve what I wanted with an InputStream.
InputStream is = getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
To work, the imageUri must be an Uri along the lines of:
content://com.android.externalstorage.documents/document/primary:Download/image.png
However, this means you will be accessing files you do not have permission by default. This means you will have to deal with getting permission.
This is not what I wanted so I scrapped this approach.

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)

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

How do I get the URI of a file saved with FileOutputStream?

Here's the code that saves an image file to... somewhere? How do I get the URI for the file "webimage"?
Bitmap bmp = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
String fileName = "webImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
Use getFileStreamPath() like this:
String fileName = "webImage";
//...
Uri uri = Uri.fromFile(getFileStreamPath(fileName));
Figured it out I think:
final File file = new File(getFilesDir(), "webImage");
Uri weburi = Uri.fromFile(file);
You can use Uri.fromFile(imageFile), where imageFile is an instance of File

How do I open a file I have the Uri for in Android?

I am trying to open an image I have stored on external memory. Here is the code I have:
File imagePath = new File(imageURI);
InputStream inputStream=null;
try {
inputStream = getContentResolver().openInputStream(Uri.parse(imageURI));
}catch(FileNotFoundException e){
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] originalImage = baos.toByteArray();
But it doesn't seem to be able to locate the file. The Uri is in the format content://com.android.providers.media.documents/document/image%3A21.
Thanks for any help.
In a project I am working on now I have external images in a directory on the SD card. I am using
String thePath = Environment.getExternalStorageDirectory() + "/myAppFiles”;
File imgFile = new File(thePath + " / " + "
externalImage.jpg ");
if (imgFile.exists()) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
}
}
Try this:
File imagePath = new File(imageURI.getPath());
url.getPath() returns a String in the following format: "/mnt/sdcard/xxx.jpg", without the scheme type pre-fixed

Categories