Reputation: 1643
This should be a lot easier than it has been... but I'm just trying to get the full path for an image from URI.
String[] projection = { MediaStore.MediaColumns.DISPLAY_NAME};
Will correctly display the name of the file but:
String[] projection = { MediaStore.MediaColumns.DATA};
or...
String[] projection = { MediaStore.Images.Media.DATA};
...will only give me NULL.
Maybe I'm just too tired and not thinking straight (this is a VERY small part of a much larger app) but I can't quite understand how that would be possible if the image is clearly there, the Bitmap is working fine, and the display name works. I'm sure I'm missing something easy... I just can't see it at the moment.
The full code is below:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
Uri selectedImageUri = null;
if (data != null) {
selectedImageUri = data.getData();
String[] projection = { MediaStore.MediaColumns.DISPLAY_NAME};
ContentResolver cr = this.getApplicationContext().getContentResolver();
Cursor metaCursor = cr.query(selectedImageUri,
projection, null, null, null);
metaCursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
if (metaCursor != null) {
try {
if (metaCursor.moveToFirst()) {
imagepath = metaCursor.getString(0);
Log.i(TAG, "UriB: " + metaCursor.getString(0));
}
} finally {
metaCursor.close();
}
}
Log.i(TAG, "Uri: " + selectedImageUri.toString());
}
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImageUri));
imageview.setImageBitmap(bitmap);
messageText.setText("Uploading file path: " + imagepath);
} catch (IOException ie) {
messageText.setText("Error");
}
}
}
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jon.bon" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessageActivity" >
</activity>
</application>
I know there are a lot of pages on this because I have about 7 of them open right now, but I just can't get why I'm stuck.
EDIT
It looks like I might have to use EXTERNAL_CONTENT_URI. But when I do this:
Cursor metaCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
...I get some completely random (well... actually ONE very specific) path no matter what image I try to work with. Like it isn't related at all.
Upvotes: 1
Views: 1887
Reputation: 21053
If permission is enable code below will work for 'Intent.ACTION_PICK'.
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, Constants.PICK_GALLERY);
If not then add the Device specification . OnActivity Result as follows .
if(responseCode == activity.RESULT_OK && requestCode==Constants.PICK_GALLERY){
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
try {
Cursor c = activity.getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
if(picturePath==null) {
picturePath=selectedImage.getPath();
}
//Use picturePath for setting bitmap or to crop
}catch(Exception e)
{
e.printStackTrace();
}
}
Upvotes: 2