Renaming Video / Image While Targeting Android 11 (Api 30) - java

I'm having difficulty simply renaming a file created by the app but was put into the documents folder.
EDIT:
As it so happens the videos are not created by the application but are expected to be renamed by the application. The user drops the videos into the documents folder manually at the start. My mistake.
Here is my code:
public static boolean renameVideoFile(Context c, File from, File to) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
Uri fromUri = FileProvider.getUriForFile(c, c.getPackageName() + ".provider", new File(FileUtils.getVideosDir(), from.getName()));
ContentResolver contentResolver = c.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, to.getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
} else {
if (from.renameTo(to)) {
removeMedia(c, from);
addMedia(c, to);
return true;
} else {
return false;
}
}
}
I progressed through a few errors but my final error is:
java.lang.UnsupportedOperationException: No external updates
Which is an internal problem with the FileProvider at
at androidx.core.content.FileProvider.update(FileProvider.java:523)
EDIT #2
Also here are my provider declarations in the manifest:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths">
</meta-data>
</provider>
And here is my pathing declarations. Again this is causing no issues for saving:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="internal_images"
path="files/Pictures" />
<external-files-path
name="internal_images_alternate"
path="Pictures" />
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>

EDIT: The external folder I chose was the documents folder FYI
So I did finally get it working. Here is the code to rename a video (it may not be the best but it does the trick!)
private static void tryAddVideosToMediaStore(Activity context) {
List<File> files = MediaUtils.getVideoFilesFromDirectory();
for (File file : files) {
try {
Uri fromUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
if (getRealPathFromURI(context, fromUri) == null) {
String nameWoExtension = MediaUtils.getNameWithoutStatus(file.getAbsolutePath());
ContentValues values = new ContentValues(3);
values.put(MediaStore.Video.Media.TITLE, nameWoExtension);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, file.getAbsolutePath());
context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch(Exception e) {
return null;
}finally {
if (cursor != null) {
cursor.close();
}
}
}
And then the calling methods
public static String getVideoNameFromPath(String path) {
return path.substring(path.lastIndexOf("/") + 1, path.indexOf(".mp4"));
}
public static boolean renameVideoFile(MainActivityViewModel viewModel, SharedPreferenceHelper sharedPreferenceHelper, Activity c, File from, File to) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
tryAddVideosToMediaStore(c);
Uri fromUri = MediaUtils.getVideoUriFromFS(c, from);
try {
ContentResolver contentResolver = c.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, to.getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
return true;
} catch (Exception securityException) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
sharedPreferenceHelper.get().edit().putString("from", from.getAbsolutePath()).putString("to", to.getAbsolutePath()).apply();
RecoverableSecurityException recoverableSecurityException;
viewModel.setContentUri(fromUri);
if (securityException instanceof RecoverableSecurityException) {
recoverableSecurityException =
(RecoverableSecurityException) securityException;
} else {
requestVideoWritePermissions(c, Uri.parse(MediaStore.Video.Media.EXTERNAL_CONTENT_URI + "/" + MediaUtils.getVideoId(c, from)));
return false;
}
IntentSender intentSender = recoverableSecurityException.getUserAction()
.getActionIntent().getIntentSender();
try {
c.startIntentSenderForResult(intentSender, 55,
null, 0, 0, 0);
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
throw new RuntimeException(
securityException.getMessage(), securityException);
}
}
return false;
} else {
if (from.renameTo(to)) {
removeMedia(c, from);
addMedia(c, to);
return true;
} else {
return false;
}
}
}
public static Uri getVideoUriFromFS(Context c, File file) {
long id = getFilePathToMediaID(file, c);
Uri fromUri = ContentUris.withAppendedId( MediaStore.Video.Media.EXTERNAL_CONTENT_URI,id);
return fromUri;
}
public static long getFilePathToMediaID(File videoPath, Context context)
{
Uri mainUri;
Cursor cursor1 = context.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Video.Media._ID},
MediaStore.Video.Media.DATA + "=? ",
new String[]{videoPath.getAbsolutePath()}, null);
long id = 0;
if (cursor1 != null && cursor1.moveToFirst()) {
id = cursor1.getLong(cursor1.getColumnIndex(MediaStore.MediaColumns._ID));
cursor1.close();
}
return id;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 55) { //rename video request code
if (resultCode == RESULT_OK) {
//update UI
String from = presenter.getFromFilePath();
String to = presenter.getToFilePath();
if (from != null && to != null) {
Uri fromUri = MediaUtils.getVideoUriFromFS(this, new File(from));
ContentResolver contentResolver = getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 1);
contentResolver.update(fromUri, contentValues, null, null);
contentValues.clear();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, new File(to).getName());
contentValues.put(MediaStore.Files.FileColumns.IS_PENDING, 0);
contentResolver.update(fromUri, contentValues, null, null);
//update UI
}
}
}
}
If I forgot something please let me know and I will post it here. It took literally hours of searching to find this solution. I'm quite upset at the simplicity vs the complexity that google has introduced.
EDIT: I Think I forgot this method which was very important
public static boolean requestVideoWritePermissions(Activity activity, Uri fromUri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
boolean hasPermission = true;
if (activity.checkUriPermission(fromUri, Binder.getCallingPid(), Binder.getCallingUid(),
Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
hasPermission = false;
}
List<Uri> uriList = new ArrayList<>();
uriList.add(fromUri);
if (!hasPermission) {
PendingIntent pi = MediaStore.createWriteRequest(activity.getContentResolver(), uriList);
try {
activity.startIntentSenderForResult(pi.getIntentSender(), 55, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
return false;
}
return true;
}
return true;
}
I should also mention each video is a prompt this way. The user chooses whether or not to allow you to overwrite each video which was less than optimal. I wish I could just do a whole folder of external access but I'm guessing that's not going to happen with scoped storage changes.

Since I see this is a very popular question, I'm going to go ahead and update what I changed to be doing since some of this code is deprecated or not working.
First In Your build.gradle file, implement the SAF framework's DocumentFile class:
implementation 'androidx.documentfile:documentfile:1.0.1'
Next Call this method which request permissions for the SAF to operate (You will only need to do this once on user install):
private void requestDocumentTreePermissions() {
// Choose a directory using the system's file picker.
new AlertDialog.Builder(this)
.setMessage("*Please Select A Folder For The App To Organize The Videos*")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.Q)
#Override
public void onClick(DialogInterface dialog, int which) {
StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
Intent intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
String startDir = "Documents";
Uri uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI");
String scheme = uri.toString();
scheme = scheme.replace("/root/", "/document/");
scheme += "%3A" + startDir;
uri = Uri.parse(scheme);
Uri rootUri = DocumentsContract.buildDocumentUri(
EXTERNAL_STORAGE_PROVIDER_AUTHORITY,
uri.toString()
);
Uri treeUri = DocumentsContract.buildTreeDocumentUri(
EXTERNAL_STORAGE_PROVIDER_AUTHORITY,
uri.toString()
);
uri = Uri.parse(scheme);
Uri treeUri2 = DocumentsContract.buildTreeDocumentUri(
EXTERNAL_STORAGE_PROVIDER_AUTHORITY,
uri.toString()
);
List<Uri> uriTreeList = new ArrayList<>();
uriTreeList.add(treeUri);
uriTreeList.add(treeUri2);
getPrimaryVolume().createOpenDocumentTreeIntent()
.putExtra(EXTRA_INITIAL_URI, rootUri);
Intent intent2 = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when it loads.
intent2.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
intent2.putExtra(EXTRA_INITIAL_URI, rootUri);
startActivityForResult(intent2, 99);
}
})
.setCancelable(false)
.show();
}
Next Store some Persistant Permissions:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 99 && resultCode == RESULT_OK) {
//get back the document tree URI (in this case we expect the documents root directory)
Uri uri = data.getData();
//now we grant permanent persistant permissions to our contentResolver and we are free to open up sub directory Uris as we please until the app is uninstalled
getSharedPreferences().edit().putString(ACCESS_FOLDER, uri.toString()).apply();
final int takeFlags = (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getApplicationContext().getContentResolver().takePersistableUriPermission(uri, takeFlags);
//simply recreate the activity although you could call some function at this point
recreate();
}
}
Finally call the documentFile's rename method on the correct file
DocumentFile df = DocumentFile.fromTreeUri(MainActivity.this, uri);
df = df.findFile("CurrentName")
df.renameTo("NewName");
You Can also open InputStreams and OutputStreams using your content resolver because of the persistant URI permissions granted to your content resolver for that DocumentFile using the following snippet:
getContentResolver().openInputStream(df.getUri());
getContentResolver().openOutputStream(df.getUri());
You can list files using
df.listFiles();
Or You can list out files using:
public static DocumentFile findFileInDirectoryMatchingName(Context mContext, Uri mUri, String name) {
final ContentResolver resolver = mContext.getContentResolver();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(mUri,
DocumentsContract.getDocumentId(mUri));
Cursor c = null;
try {
c = resolver.query(childrenUri, new String[]{
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_MIME_TYPE,
DocumentsContract.Document.COLUMN_LAST_MODIFIED
}, DocumentsContract.Document.COLUMN_DISPLAY_NAME + " LIKE '?%'", new String[]{name}, null);
c.moveToFirst();
while (!c.isAfterLast()) {
final String filename = c.getString(1);
final String mimeType = c.getString(2);
final Long lastModified = c.getLong(3);
if (filename.contains(name)) {
final String documentId = c.getString(0);
final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(mUri,
documentId);
return DocumentFile.fromTreeUri(mContext, documentUri);
}
c.moveToNext();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
return null;
}
Which will run faster than the df.listFiles() method

Related

Files not being deleted in android 11 using MediaStore.createDeleteRequest()

I am trying to delete audio recordings that I created before Re-installing my app. I'm using MediaStore.createDeleteRequest() and it successfully shows me a dialog box to ask for permission to delete the files, but when I click "Allow" it doesn't delete the files.
My Audio Recordings are stored in "storage/emulated/0/MUSIC/Wear Voice Recorder/"
This is my code :
public void onClick(View v) {
List<Uri> uris = new ArrayList<>();
for (Recordings rec : selectionList) {
String date = rec.getRecordingDate();
SimpleDateFormat original = new SimpleDateFormat("d MMM yy, hh:mm:ss a");
SimpleDateFormat target = new SimpleDateFormat("yyyyMMdd_HHmmss");
try {
tempDate = original.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
String fileName = rec.getRecordingName() + "_W_" + target.format(tempDate) + ".mp3";
File directory = Environment.getExternalStorageDirectory();
file = new File(directory + File.separator + Environment.DIRECTORY_MUSIC + File.separator + "Wear Voice Recorder");
File[] list = file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".mp3");
}
});
for (File mediaFile : list) {
if (mediaFile.getName().equals(fileName)) {
arrList.remove(rec);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
long mediaID = getFilePathToMediaID(mediaFile.getPath(), RecordingsListActivity.this);
Uri Uri_one =ContentUris.withAppendedId(MediaStore.Audio.Media.getContentUri("internal"), mediaID);
uris.add(Uri_one);
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
try {
mediaFile.delete();
} catch (Exception e) {
Toast.makeText(RecordingsListActivity.this, "Recording Not Found", Toast.LENGTH_SHORT).show();
}
}
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
requestDeletePermission(RecordingsListActivity.this, uris);
System.out.println(uris+"");
}
adapter.notifyDataSetChanged();
endSelectionMode();
}
#RequiresApi(api = Build.VERSION_CODES.R)
private void requestDeletePermission(Context context, List<Uri> uri_one) {
PendingIntent pi = MediaStore.createDeleteRequest(context.getContentResolver(), uri_one);
try {
startIntentSenderForResult(pi.getIntentSender(), REQUEST_PERM_DELETE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
private long getFilePathToMediaID(String path, Context context) {
long id = 0;
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("internal");
String selection = MediaStore.Audio.Media.DATA;
String[] selectionArgs = {path};
String[] projection = {MediaStore.Audio.Media._ID};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, null);
if (cursor != null) {
while (cursor.moveToNext()) {
int idIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
id = Long.parseLong(cursor.getString(idIndex));
}
}
return id;
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_PERM_DELETE:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(RecordingsListActivity.this, "Deleted successfully!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RecordingsListActivity.this, "Failed to delete!", Toast.LENGTH_SHORT).show();
}
break;
}
}
I don't really know much about MediaStore, this is my first app and it's so frustrating to ask for permission to delete files that my app created before I uninstalled and re-installed.
I think there's something wrong with the URI, when I print the URI of different files, the URIs are the same.
It does show me the dialog box to delete the files and it also shows a toast saying "Deleted Successfully!" but the files are still there.
Uri uri = MediaStore.Files.getContentUri("internal");
Try:
Uri uri = MediaStore.Video.Media.getContentUri("internal");
But probably you should change "internal" to MediaStore.VOLUME_EXTERNAL too.

How to edit an image with absolute path?

I have use the pesdk library to edit an image.
public static int GALLERY_RESULT = 2;
private void selectImgtoEdit() {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(path));
startActivityForResult(intent, GALLERY_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Uri selectedImage = intent.getData();
openEditor(selectedImage);
}
public void openEditor(Uri inputImage) {
SettingsList settingsList = createPesdkSettingsList();
// Set input image
settingsList.getSettingsModel(LoadSettings.class).setSource(inputImage);
settingsList.getSettingsModel(PhotoEditorSaveSettings.class).setOutputToGallery(Environment.DIRECTORY_DCIM);
new PhotoEditorBuilder(this).setSettingsList(settingsList).startActivityForResult(this, PESDK_RESULT);
}
I want to use that library with the absolute path of an image. How can I setting it ?
You should convert absolute path to content uri.
For example
Below Android P
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
cursor.close();
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
Above Android Q
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media._ID},
MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
cursor.close();
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
if (imageFile.exists()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = context.getContentResolver();
Uri picCollection = MediaStore.Images.Media
.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
ContentValues picDetail = new ContentValues();
picDetail.put(MediaStore.Images.Media.DISPLAY_NAME, imageFile.getName());
picDetail.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
picDetail.put(MediaStore.Images.Media.RELATIVE_PATH,"DCIM/" + UUID.randomUUID().toString());
picDetail.put(MediaStore.Images.Media.IS_PENDING,1);
Uri finaluri = resolver.insert(picCollection, picDetail);
picDetail.clear();
picDetail.put(MediaStore.Images.Media.IS_PENDING, 0);
resolver.update(picCollection, picDetail, null, null);
return finaluri;
}else {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
} else {
return null;
}
}
}
Do you have the absolute path of the image?
I suggest code like below, try again...
String filepath = "/storage/emulated/0/image.jpg";
Uri selectedUri = getImageContentUri(context, new File(filepath));
openEditor(selectedUri);
Use second getImageContentUri function in above answer.

Return 404 (FILE_NOT_FOUND) on upload to OwnCloud with OwnCloud Android API

I created my OwnCloud server with Oracle VirtualBox Debian. I want my Android application to upload image to the cloud. So i created intent to get image from the Gallery and to upload it with the OwnCloud Android library from GitHub: https://github.com/owncloud/android-library
. I followed the steps from the sample but every time when i start uploading, the result is 404 - FILE_NOT_FOUND
Here is a snippet of my code:
floatingActionButton.setOnClickListener(v -> {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType(IMAGE_INTENT);
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
});
}
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
profilePicture.setImageBitmap(selectedImage);
File file = new File(getRealPathFromURI(imageUri));
Log.d("Image file", file.getAbsolutePath() + "\t" + file.getName());
handler = new Handler();
Uri serverUri = Uri.parse("http://192.168.1.8/remote.php/webdav/");
client = OwnCloudClientFactory.createOwnCloudClient(serverUri, this, true);
client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials("user", "bitnami"));
client.getParams().setAuthenticationPreemptive(true);
String remotePath = FileUtils.PATH_SEPARATOR + file.getName();
String mimeType = "image/png";
// Get the last modification date of the file from the file system
Long timeStampLong = file.lastModified() / 1000;
String timeStamp = timeStampLong.toString();
UploadRemoteFileOperation uploadOperation = new UploadRemoteFileOperation(file.getAbsolutePath(), remotePath, mimeType, timeStamp);
uploadOperation.addDatatransferProgressListener(this);
uploadOperation.execute(client, this, handler);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
#Override
public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileAbsoluteName) {
final long percentage = (totalToTransfer > 0 ? totalTransferredSoFar * 100 / totalToTransfer : 0);
handler.post(() -> Log.d("Progress", "progressRate " + percentage));
}
#Override
public void onRemoteOperationFinish(RemoteOperation caller, RemoteOperationResult result) {
Toast.makeText(this, String.valueOf(result.getHttpCode()), Toast.LENGTH_SHORT).show();
}

Android KitKat Image selecting not returning anything

I've been trying to get the absolute image path of a image selected in the gallery on KitKat but it doesn't seem to be working out. My variable IMAGE_FILEPATH is always "" after whatever I do. Here's the code for my onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) return;
if (null == data) return;
Uri originalUri = null;
if (requestCode == 1) {
//JB!!!
Uri uri = data.getData();
if (uri != null) {
try {
// User had pick an image.
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver()
.query(uri,
filePathColumn, null, null, null);
cursor.moveToFirst();
IMAGE_FILEPATH = cursor.getString(0);
cursor.close();
} catch (Exception e) {
Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
}
}
} else if (requestCode == 2) {
//KK!!!
Uri uri = data.getData();
if (uri != null) {
try {
if( uri == null ) {
IMAGE_FILEPATH = uri.getPath();
} else {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
IMAGE_FILEPATH = cursor.getString(column_index);
} else {
IMAGE_FILEPATH = uri.getPath();
}
}
} catch (Exception e) {
Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
}
}
}
Resource.toast(IMAGE_FILEPATH);
super.onActivityResult(requestCode, resultCode, data);
}
What's going wrong? I've tried multiple solutions and nothing seems to work.
In KitKat the Gallery returns an URI like this : content://com.android.providers.media.documents/document/image:1
instead of :
content://media/external/images/media/1
So, here is what you can write under KK to make it works :
if (uri != null) {
try {
if( uri == null ) {
IMAGE_FILEPATH = uri.getPath();
} else {
// get the id of the image selected by the user
String wholeID = DocumentsContract.getDocumentId(data.getData());
String id = wholeID.split(":")[1];
String[] projection = { MediaStore.Images.Media.DATA };
String whereClause = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().query(getUri(), projection, whereClause, new String[]{id}, null);
if( cursor != null ){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
IMAGE_FILEPATH = cursor.getString(column_index);
}
cursor.close();
} else {
IMAGE_FILEPATH = uri.getPath();
}
}
} catch (Exception e) {
Crouton.makeText(this, "Failed to get image", Style.ALERT).show();
}
}
And the function I used :
private Uri getUri() {
String state = Environment.getExternalStorageState();
if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
}
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
These posts helped me : retrieve absolute path when select image from gallery kitkat android and Get real path from URI, Android KitKat new storage access framework
moveToFirst returns a boolean indicating if moving to that position resulted in data.
boolean haveData;
haveData = cursor.moveToFirst();
if (haveData) {
cursor.getString(.....
That's what you should be doing, which is just good, defensive programming. Now, why your cursor is empty, that's going to take more digging to your contentResolver.

Get filepath and filename of selected gallery image in Android

I am creating an app which uploads a selected image from the gallery and uploads it to a web service. The webservice requires the filename of selected image plus a base64 encoding of the file contents. I have managed to achieve this with a hardcoded file path. However, I am struggling to get the real filepath of the image. I have read around the web and have this code, but it does not work for me:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
try {
Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Log.d("Picture Path", picturePath);
}
catch(Exception e) {
Log.e("Path Error", e.toString());
}
}
}
I get this error:
java.lang.NullPointerException
EDIT
Forgot to mention I am using Kitkat. It looks like my problem is KitKat related. I found this (see below) which helped me get my app working:
Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT
A little late to the party but here's my code, hope this helps.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData( );
String picturePath = getPath( getActivity( ).getApplicationContext( ), selectedImageUri );
Log.d("Picture Path", picturePath);
}
}
public static String getPath( Context context, Uri uri ) {
String result = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
if(cursor != null){
if ( cursor.moveToFirst( ) ) {
int column_index = cursor.getColumnIndexOrThrow( proj[0] );
result = cursor.getString( column_index );
}
cursor.close( );
}
if(result == null) {
result = "Not found";
}
return result;
}
Use below code,
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String s = getRealPathFromURI(selectedImageUri);
editText1.setText(s);
}
}
and below is your getRealPathFromURI function,
public String getRealPathFromURI(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Try this:
Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));
And
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
public class RealFilePath {
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* #param context The context.
* #param uri The Uri to query
*/
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)
);
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}
else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* #param context The context.
* #param uri The Uri to query.
* #param selection (Optional) Filter used in the query.
* #param selectionArgs (Optional) Selection arguments used in the query.
* #return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
}
finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* #param uri The Uri to check.
* #return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
If you are like me and you dont have a local file ready, for instance you want to give user a chance to take a picture and then upload the picture this is how to do it.
I am a noob in java but i have allot of experience in coding.
Stackoverflow helped me allot so now it is my time to do something back.
Under class activity you have to declare these two items.
asuming you have a working upload class and php script.
NOTE i am not taking any consideration about all the different API's
out there. I am using java compiler 1.7 and a API range of 10 to 18.
public class Yourproject extends Activity {
File imageFile;
File imageFileName;
//This part is needed for decoding the path adres.
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
//This is needed for the path name, now you might just want to use one decoding script depending on //your class in total.
//In my case this was the way to go.
//To use one single addres use this line ImageColumns.DATA in the int idx declaration.
private String getRealPathFromNAME(Uri contentNAME) {
String result;
Cursor cursor = getContentResolver().query(contentNAME, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentNAME.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
//Then under protected void onCreate(Bundle savedInstanceState) you place this code to get
//the image and process it.
#Override
protected void onCreate(Bundle savedInstanceState) {
//declare the ImageView
final ImageView inputPicture = (ImageView) findViewById(R.id.imageView2);
inputPicture.setImageURI((Uri) getIntent().getExtras().get("picture"));
//here where recieve the image from last page "Putextra" method
try {
//here we will get the data and proces it trough to get the real
//address for uploading.
//This is send from a other activity with a Intent.EXTRA_STREAM
//The code looks like this:
/*********"Putextra"*********/
//final ImageView iv = (ImageView) findViewById(R.id.imageView1);
//final Uri thumb = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
//iv.setImageURI(thumb);
//Sending it to the next activity
//it.putExtra("picture", thumb);
/*********"Putextra"*********/
Uri fileUri = (Uri) getIntent().getExtras().get("picture");
//get
//Uri fileName = (Uri) getIntent().getExtras().get("picture");
//get
imageFile = new File(getRealPathFromURI(fileUri));
//send
imageFileName = new File(getRealPathFromNAME(fileUri));
//send
//i got a exeption Uri fileName, use fileUri twice..
} catch (Exception e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//here we will declare the name for furter processing.
final File uploadFilePath = imageFile;
final File uploadFileName = imageFileName;
}
And the rest i will leave up to u, if you got this far, I am pretty sure you will finish.
Try this,
file = new File(getPath(outputFileUri));
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Hope this helps you
if (Build.VERSION.SDK_INT <19){
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
}else{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) return;
if (null == data) return;
Uri originalUri = null;
if (requestCode == GALLERY_INTENT_CALLED) {
originalUri = data.getData();
}
else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
originalUri = data.getData();
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
}
loadSomeStreamAsynkTask(originalUri);
}
Try this to get data as well as file path and insert into folder.
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == RESULT_OK)
{
nav = data.getData();
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(nav, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path= cursor.getString(column_index);
Toast.makeText(getApplicationContext(), path, 500).show();
mBitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), nav);
insert(mBitmap);
if (mBitmap != null) {
mBitmap.recycle();
mBitmap = null;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void insert(Bitmap finalBitmap) {
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
File dstFile = new File(file.getParent(), "." + file.getName());
file.renameTo(dstFile);
} catch (Exception e) {
e.printStackTrace();
}
}

Categories