I have a Download folder in which there are many images. I want to get a single image from that folder and show into ImageView. Code is given below I am using this code to onCreate() method. So that there is no need to choose from the folder. I have already given required permission. But getting java.io.FileNotFoundException: Multiple items at content://media/external/images/media at given code.
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg);
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Uri selectedImage = intent.getData();
String[] filePaths = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePaths, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePaths[0]);
String picturePath = c.getString(columnIndex);
Log.e(TAG, "onActivityResult: "+picturePath );
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
try {
Uri selectedimg = intent.getData();
image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedimg);
Bitmap resizedBmp = Helpers.resizeForPreview(image);
selectImage.setImageBitmap(resizedBmp);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("path of image ", picturePath + " "+ Uri.parse(picturePath));
selectImage.setImageBitmap(thumbnail);
Related
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.
I'm getting images from uris, but they aren't showing any of the gallery effects that have been made (ie. grayscale, sepia). I'm hoping to get the edited images. For example:
ContentResolver cr = activity.getContentResolver();
Cursor cur = cr.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // data
new String[] { MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME }, // Which columns to return
"", // Which rows to return (all rows)
null, // Selection arguments (none)
MediaStore.Images.Media.DATE_ADDED+" DESC" // Ordering
);
ArrayList<String> bucketImageList = new ArrayList<String>();
//get bitmap thumbnails for all albums
if (cur.moveToFirst()) {
String bucketName;
String imageID;
do {
imageID = cur.getString(cur.getColumnIndex(MediaStore.Images.Media._ID));
Uri uri = Uri.parse("content://media/external/images/media");
uri = Uri.withAppendedPath(uri, "" + imageID);
bucketImageList.add(uri.toString());
} while (cur.moveToNext());
cur.close();
bucketImages = bucketImageList.toArray(new String[bucketImageList.size()]);
}
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(bucketImages[2]));
This bitmap will not show any effects made in the Gallery app (Except the Motorola Gallery app).
Your code should look similar to this:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
}
If you need to load very large images, the following code will load it in in tiles (avoiding large memory allocations):
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(myStream, false);
Bitmap region = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
Also see this answer
Trying to set images to image view using the image paths.
My image path
String img_path ="/storage/sdcard0/Fbt/xylo.jpg";
image path code
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.Media.DATA);
return cursor.getString(idx);
}
}
selectedImage = imageReturnedIntent.getData();
File imageFile = new File(getRealPathFromURI(selectedImage));
String path= imageFile.toString();
output =/storage/sdcard0/Fbt/xylo.jpg
I tried all possible code still there is no success
ImageView carpict =(ImageView)findViewById(R.id.img);
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
carpict.setImageBitmap(bitmap);
2.
Uri image22 =(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+img_path));
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(image22);
Bitmap carpic = BitmapFactory.decodeStream(imageStream);
// carpic = Bitmap.createScaledBitmap(carpic, 72,72, false);
carpict.setImageBitmap(carpic);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3rd.
carpict.setImageURI
(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+img_path));
4th.
File imgFile =new File("img_path");
carpict.setImageBitmap(BitmapFactory.decodeFile(imgFile.getAbsolutePath()));
I have also added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
can anyone tell me where i am going wrong?
try this code
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
System.out.println("sdcard path:"+Environment.getExternalStorageDirectory());
Drawable d = Drawable.createFromPath(Environment.getExternalStorageDirectory()+"/images.png");
imageView.setBackground(d);
imageView.setImageURI(Uri.parse(new File("PathToFile"));
or if you have the file reference:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+"/Fleet/xylo.jpg");
imageView.setImageURI(Uri.parse(file));
This should work. You will need to use the file constructor version of Uri.parse to correctly set the image.
Do check if the image is present in this location, or if the image is valid. And please post the logs if this also doesn't work.
The problem was with the image itself when i tried with other images it worked images with formats .jpg .png .jpeg
ImageView carpict =(ImageView)findViewById(R.id.img);
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
carpict.setImageBitmap(bitmap);
Here is the image xylo.jpg which is not getting set, don't know why
http://www.mediafire.com/?lh75aco1en8f3ot
Stirng path="///storage/sdcard0/Android/data/com.package.name/files/Download/Images/one-3.jpg"; //chang with your image file path
File file = new File(path);
imageView.setImageURI(Uri.parse(file.getPath()));
Is it possible to get the video thumbnail PATH, not Bitmap object itself? I'm aware of method
MediaStore.Images.Thumbnails.queryMiniThumbnail
but since I use my own Bitmap caching mechanism I want to have the path to video thumbnail rather than the Bitmap object itself. This method returns Bitmap object, not path.
Thanks
First get the video file URL and then use below query.
Sample Code:
private static final String[] VIDEOTHUMBNAIL_TABLE = new String[] {
Video.Media._ID, // 0
Video.Media.DATA, // 1 from android.provider.MediaStore.Video
};
Uri videoUri = MediaStore.Video.Thumbnails.getContentUri("external");
cursor c = cr.query(videoUri, VIDEOTHUMBNAIL_TABLE, where,
new String[] {filepath}, null);
if ((c != null) && c.moveToFirst()) {
VideoThumbnailPath = c.getString(1);
}
VideoThumbnailPath, should have video thumbnail path. Hope it help's.
Get Video thumbnail path from video_id:
public static String getVideoThumbnail(Context context, int videoID) {
try {
String[] projection = {
MediaStore.Video.Thumbnails.DATA,
};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Video.Thumbnails.VIDEO_ID + "=?",
new String[] { String.valueOf(videoID) },
null);
cursor.moveToFirst();
return cursor.getString(0);
} catch (Exception e) {
}
return null;
}
I have used this code to start an intent to pick a picture from the tablet's gallery.
Intent pick_picture_intent = new Intent(Intent.ACTION_GET_CONTENT);
pick_picture_intent.setType("image/*");
startActivityForResult(pick_picture_intent, PICK_PICTURE);
Everything works fine, with the startActivityForResult(), having the image placed into a image view. (ONLY IN LANDSCAPE MODE), however, when I try doing the same thing in PORTRAIT MODE, it doesn't work.
Code in startActivityFroResult():
if(data != null && data.getData() != null) {
Uri uri = data.getData();
}
Cursor cursor = getContentResolver().query(uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
try {
String imagePath = cursor.getString(0);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ImageView imageView = new ImageView(context);
imageView.setImageBitmap(bitmap);
myLayout.addView(imageView);
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
}
Why is this happening?
I was thrown this error:
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here