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()));
Related
I have two Image-View in a layout, one as at background and another is above that(at foreground) and I want to save both the images. So can anyone help me out that how can I save both the images into the device storage as single image.
Thank you.
A simple solution I found here is to put both of your imageView in a single Layout and then save your layout as a Bitmap. I will retype the solution code here
private Bitmap getBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Toast.makeText(StopWarApp.getContext(), "Something went wrong",
Toast.LENGTH_SHORT).show();
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
Now that you have your Bitmap, you can save it to your storage like this
private void saveImage(Bitmap finalBitmap, String image_name) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
myDir.mkdirs();
String fname = "Image-" + image_name+ ".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();
} catch (Exception e) {
e.printStackTrace();
}
}
Don't forget to add your permissions in the manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
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);
I have an image to the right and a button "download" to the left. the image is from my drawable. now,when i try to click the download i want to put the image to my sdcard downloads. Please help me i only see about download in url. is there other solution for this . Thanks
public class ImageDownloader {
public void download(String url, ImageView imageView) {
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
task.execute(url);
}
}
/* class BitmapDownloaderTask, see below */
}
First, you need to get your Bitmap. You can already have it as an object Bitmap, or you can try to get it from the ImageView such as:
BitmapDrawable drawable = (BitmapDrawable) ImageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Then you must get to directory (a File object) from SD Card such as:
File sdCardDirectory = Environment.getExternalStorageDirectory();
Next, create your specific file for image storage:
File image = new File(sdCardDirectory, "test.png");
After that, you just have to write the Bitmap such as:
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();
}
Finally, just deal with the boolean result if needed. Such as:
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
Don't forget to add the following permission in your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
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.
So I am new to Android, but I do not understand why this does not work (and how to get it to work):
ImageView i = (ImageView) findViewById(R.id.image_to_display);
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
i.setImageURI(uri)
I did a Toast and have made sure that uri.toString() returns a url like content://...
I am also sure that i is a valid reference, because I am successfully able to set it to local images that are part of the .apk.
So why would this not work, and how can I get it to work?
Thanks
If you don't HAVE to have it be an Uri you could do something like this instead.
String fullpath = Environment.getExternalStorageDirectory() + "/pathtoyourfile"
// take the path create a bitmap and populate the ImageView with it.
ImageView iv = (ImageView) v.findViewById(R.id.thumbnail);
Bitmap bm = BitmapFactory.decodeFile(fullpath);
iv.setImageBitmap(bm);
Try loading it yourself and then passing it in:
ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
if (bitmap != null) {
i.setImageBitmap(bitmap);
}