I am accessing pictures of the device's gallery via my app, when the picture is accessed the metadata of the picture will be read and stored in metadata. The problem I'm facing is that whenever the program tries to read the metadata I'm getting the following error java.io.FileNotFoundException: /storage/emulated/0/Snapchat/Snapchat-1185425082.jpg (Permission denied).
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
imageView.setImageURI(imageUri);
File picturesfile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
picturesfile.setReadable(true);
picturesfile.setExecutable(true);
String[] projection = {MediaStore.Images.Media.DATA};
try {
Cursor cursor = getContentResolver().query(imageUri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
path = cursor.getString(columnIndex);
Log.d("Picture Path", path);
}
catch(Exception e) {
Log.e("Path Error", e.toString());
}
File jpegFile = new File(path);
jpegFile.setReadable(true);
jpegFile.setExecutable(true);
try {
metadata = ImageMetadataReader.readMetadata(jpegFile);
for (Directory directory : metadata.getDirectories()) {
for (Tag hoi : directory.getTags()) {
Log.d("tags ", hoi.toString());
}
}
} catch (ImageProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(resultCode == RESULT_OK && requestCode == 0){
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
}
Tactically, it would appear that you do not have the READ_EXTERNAL_STORAGE permission, which you need to request in the manifest and at runtime.
Beyond that:
There is no requirement for your query() to return a DATA column
There is no requirement that the DATA column have a value
There is no requirement that the DATA column have a filesystem path
There is no requirement that the filesystem path in the DATA column be a file that you can access, even with READ_EXTERNAL_STORAGE
In particular, it is guaranteed that your code will fail on Android Q, and it is very likely to fail for lots of users on lots of other devices as well.
Use the Uri (imageUri) with ContentResolver to get an InputStream (or perhaps a FileDescriptor) to pass to your library.
Related
I am trying to put selected files(not exclusive to images, it can be any file) from file chooser intent to a zip file. I need full file path to do this, but intent only gives uri paths.
I have tried .getPath() but that does not give the real path of the file
I have tried getRealPathFromRealURI: android get real path by Uri.getPath()
I have tried File file = new File(), file.getPath()
This is my code:
public void onActivityResult(int requestCode, int resultCode, Intent result){
if(requestCode == 111) {
if(null != result) { // checking empty selection
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
if(null != result.getClipData()) { // checking multiple selection or not
for(int i = 0; i < result.getClipData().getItemCount(); i++) {
String uri = result.getClipData().getItemAt(i).getUri().getPath();
uriList.add(uri);
Log.d("PATH: ",uri);
}
confirmationDialog();
} else {
String uri = result.getData().getPath();
uriList.add(uri);
Log.d("PATH: ",uri);
confirmationDialog();
}
}else{Toast.makeText(getApplicationContext(),
"An error has occured: API level requirements not met",Toast.LENGTH_SHORT).show();};
}
}
}
It should give the real path for example: "/sdcard/filename.example"
Instead, it gives me: "/document/9016-4ef8:filename.example"
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
System.out.println("picturePath +"+ picturePath ); //path of sdcard
Found here: Get Real Path For Uri Android
Pick / Get the file's actual path:
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "*/*"
startActivityForResult(intent, 1)
onActivityResult:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
val file = data?.data?.let {
getFileFromUri(requireContext().contentResolver, uri, requireContext().cacheDir)
}
}
}
Get File:
private fun getFileFromUri(contentResolver: ContentResolver, uri: Uri, directory: File): File {
val file =
File.createTempFile("suffix", ".prefix", directory)
file.outputStream().use {
contentResolver.openInputStream(uri)?.copyTo(it)
}
return file
}
Once we get the file, We can get the actual path of the file.
Okay, I fixed it by using another file explorer aside from the built-in file explorer, in my case I used Cx File Explorer, different file explorers return different values.
I am using google drive api contents given on GitHub for selecting folder. With below code , I could able to get folder id to transfer data but I m not able to get folder name or folder path. Can some one help over this? I tried to use asDriveFolder but did not get required info.
Here is code to get drive Id of selected folder.
public class GoogleFolderSelector extends GoogleDriveBaseActivity {
String TAG = "Google Folder Picker";
private static final int REQUEST_CODE_OPENER = 1;
#Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
.setMimeType(new String[]{DriveFolder.MIME_TYPE})
.build(getGoogleApiClient());
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.w(TAG, "Unable to send intent", e);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
switch (requestCode) {
case REQUEST_CODE_OPENER:
if (resultCode == RESULT_OK) {
DriveId driveId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
DriveFolder driveFolder = driveId.asDriveFolder();
showMessage("Folder Path"+ driveFolder);
showMessage("Selected folder's ID: " + driveId);
}
finish();
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
} catch (Exception e) {
e.printStackTrace();
Popup.longpopup("Connection Established, Click to select Folder", this);
}
}
}
In order to get folder name, let use meta data as below
Task<Metadata> getMetadataTask = getDriveResourceClient().getMetadata(file);
getMetadataTask
.addOnSuccessListener(this,
metadata -> {
showMessage(getString(
R.string.metadata_retrieved, metadata.getTitle()));
finish();
})
.addOnFailureListener(this, e -> {
Log.e(TAG, "Unable to retrieve metadata", e);
showMessage(getString(R.string.read_failed));
finish();
});
Here is the full instruction
For the folder path, I used to try to retrieve but it seems it is not necessary as you could manipulate everything with driveId such as add file into this driveId folder or create another folder inside this driveId folder. Explore google sample app will give you a better sight.
When I pick an image from gallery, I can get the Uri for that image as given below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
Uri uri = result.getData();
beginCrop(uri);
} else if (requestCode == Crop.REQUEST_CROP) {
handleCrop(resultCode, result);
}
}
The format of the Uri retrieved above is content://media/external/images/media/7266
However, I am unable to retrieve a Uri in this format when I try to fetch the Uri of an image I just saved as a file:
Date d = new Date();
CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
Bitmap bitmap = drawView.getResultBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "DCIM/Camera/" + s.toString() + ".png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
MediaScannerConnection.scanFile(getActivity(), new String[]{
image.getAbsolutePath()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
}
});
Uri uri = Uri.parse(image.getAbsolutePath());
beginCrop(uri);
The Uri obtained from above code is /storage/emulated/0/DCIM/Camera/02-04-16 12-49-16.png
I believe, this is not the correct Uri format, instead just absolute file path. Is there a way out by which I can get the Uri in the format content://media/external/images/media/ ?
Any help is much appreciated
I believe, this is not the correct Uri format, instead just absolute file path.
You are correct. Use Uri.fromFile() to convert a File into a Uri pointing to the file.
Is there a way out by which I can get the Uri in the format content://media/external/images/media/ ?
Not readily. At best, in onScanCompleted(), you might be able to run some query to get the Uri that the MediaStore uses. But, until then, MediaStore does not know about the file.
The Uri that you get from Uri.fromFile() is a valid Uri, but it will have a file scheme, not a content scheme.
you can get uri from Bitmap like this :
Uri getUri(Context context, Bitmap bitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
String path = "";
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
path = MediaStore.Images.Media.insertImage(
context.getContentResolver(), bitmap, "Title", null);
} catch (Exception e) {
}
return Uri.parse(path);
}
I want to make an app which have upload file function. But the problem is, I unable to find where did I do wrong.
First, choose the file
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
// intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
Log.d(TAG, "Select file");
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
RESULT_LOAD_FILE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(MainActivity.this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
// here
}
I guess there's no problem when choosing the file based on the logcat. But...
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, requestCode+"/"+RESULT_LOAD_FILE+"/"+resultCode+"/"+RESULT_OK);
if (data != null) Log.d(TAG, data.toString());
else Log.d(TAG, "data null");
// get file name
String fileNameSegments[] = filePath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// convert it to byte
byte[] fileByte = fileName.getBytes();
// Create the ParseFile
ParseFile file = new ParseFile(fileName, fileByte);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a New Class called "ImageUpload" in Parse
ParseObject fileupload = new ParseObject("FileUpload");
// Create a column named "ImageName" and set the string
fileupload.put("FileName", fileName);
// Create a column named "ImageFile" and insert the image
fileupload.put("DocFile", file);
// Create the class and the columns
fileupload.saveInBackground();
// Show a simple toast message
Toast.makeText(MainActivity.this, "File Uploaded", Toast.LENGTH_SHORT).show();
}
The logcat show requestCode, RESULT_LOAD_FILE, resultCode and RESULT_OK 1, 1, -1 and -1 respectively. And the data is not null, as in logcat: Intent { dat=content://com.android.externalstorage.documents/document/0A09-1112:Download/Contact n Tort.pdf flg=0x1 }
After I click the .pdf file, it triggered the toast Something went wrong but I can't find what the reason.
EDITED:
Throw null pointer exception after convert the file path to byte, when I remove the try catch block
it should be like this
Uri uri = data.getData();
// get path
filePath = uri.getPath();
// get file name
String fileNameSegments[] = filePath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// convert it to byte
byte[] fileByte = fileName.getBytes();
// Create the ParseFile
ParseFile file = new ParseFile(fileName, fileByte);
// Upload the file into Parse Cloud
file.saveInBackground();
// Create a New Class called "FileUpload" in Parse
ParseObject fileUpload = new ParseObject("FileUpload");
// Create a column named "FileName" and set the string
fileUpload.put("FileName", fileName);
Log.d(TAG, "image file");
// Create a column named "ImageFile" and insert the image
fileUpload.put("DocFile", file);
// Create the class and the columns
fileUpload.saveInBackground();
Log.d(TAG, "toast");
// Show a simple toast message
Toast.makeText(MainActivity.this, "File Uploaded",
Toast.LENGTH_SHORT).show();
though the class not created in parse dashboard but I guess that need another post.
I have an activity to choose and save a profile picture. There is an image view and a button that starts the gallery activity for result awiting the user to choose an image. When the gallery is closed, the following code is executed:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == RESULT_OK) && (requestCode == SELECT_PHOTO)) {
Uri selectedImage = data.getData();
try {
Bitmap image = this.decodeAndScaleImage(selectedImage, 285);
imgInsertPicture.setImageBitmap(image);
this.imagePresent = true;
this.saveMyProfilePicture(image);
this.popImageView();
} catch (IOException e) {
e.printStackTrace();
showToast(R.string.error_saving_picture);
}
}
}
private void saveMyProfilePicture(Bitmap picture) throws IOException {
FileOutputStream outputStream = openFileOutput(Globals.MY_PICTURE_FILE_NAME, MODE_PRIVATE);
picture.compress(Globals.MY_PICTURE_FORMAT, 90, outputStream);
outputStream.close();
ByteArrayOutputStream rawOutputStream = new ByteArrayOutputStream();
picture.compress(Globals.MY_PICTURE_FORMAT, 90, rawOutputStream);
byte[] rawPictureData = rawOutputStream.toByteArray();
rawOutputStream.close();
byte[] base64PictureData = Base64.encode(rawPictureData, Base64.DEFAULT);
rawPictureData = null;
FileOutputStream base64OutputStream = openFileOutput(Globals.MY_PICTURE_B64_FILE_NAME, MODE_PRIVATE);
base64OutputStream.write(base64PictureData);
base64OutputStream.close();
}
I debugged this code and verified that:
- no exception is thrown;
- the written files contain the exact amount of data (17kB for the jpg image, 24kB for the base64 version);
- the produced bitmap is the one that I expect and is displayed correctly in the image view.
popImageView is only used to bump the image view on top of other views that were on the front before an image was chosen; and decodeAndScale method only works on bitmap data in memory and doesn't save anything.
However, when I try to reload the current picture when the activity starts, the image displayed is blank and the jpeg file conly contains 3 bytes:
#Override
public void onStart() {
super.onStart();
if (!imagePresent && pictureExists()) {
File pictureFile = new File(getFilesDir(), Globals.MY_PICTURE_FILE_NAME);
imgInsertPicture.setImageURI(Uri.fromFile(pictureFile));
popImageView();
imagePresent = true;
}
}
Here pictureExists checks that the file name is contained in the collection returned by listFiles(). pictureFile.exists() returns true, but as I said, it conly contains 3 bytes. I also tried using BitmapFactory.decodeX, but since the file is broken, it was useless.
I cannot understand why. I checked that the file was written entirely and then it disappears...
When I was debugging on my Nexus S the code worked fine, but then I switched to a Nexus 5 and it broke.
Have you tried decoding the file to a bitmap using BitmapFactory?
http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String)
Haven't tested the following code but can you please try:
File pictureFile = new File(getFilesDir(), Globals.MY_PICTURE_FILE_NAME);
Bitmap bitmapImage = BitmapFactory.decodeFile(Uri.fromFile(pictureFile));
imgInsertPicture.setImageBitmap(bitmapImage);
popImageView();
imagePresent = true;
Try this in your onActivityResult
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
selectedImagePath =filePath;
then use selectedImagePath as file path.
Hope it helps.