Reputation: 165
I already successfully get the image from gallery but i cannot upload the image to server because the file is null. is there any code that i miss to add? i add imageView.getPath, but i only get the path from camera image to server, and get null image from gallery.
i got this path, content://com.android.providers.media.documents/document/image%3A5755 but still cannot upload to server
private void getImageFromGallery(Intent data) {
mSelectedImgURI = data.getData();
mimeType = getImageExt(mSelectedImgURI);
uploadDialog.dismiss();
imgCategory.setImageURI(mSelectedImgURI);
}
private void getImageFromCamera(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
String tempFileName = System.currentTimeMillis() + ".jpg";
File destination = new File(Environment.getExternalStorageDirectory(),tempFileName);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
mSelectedImgURI = Uri.fromFile(destination);
uploadDialog.dismiss();
imgCategory.setImageBitmap(thumbnail);
} catch (IOException e) {
Log.d(TAG, "Internal error - " + e.getLocalizedMessage());
}
}
public String getImageExt(Uri uri){
ContentResolver contentResolver = getApplicationContext().getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
Upvotes: 2
Views: 1552
Reputation: 3082
Here is a method I normally use
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
This returns the file location and you can get the file by calling
File realFile = new File(getRealPathFromURI(mSelectedImgUri));
Upvotes: 2