I am trying to read an image from /mnt/sdcard/img.jpg into ImageView.
Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/img.jpg");
imageView1.setImageBitmap(bm);
I have write external storage permission.
But ImageView is empty, LogCat don't get any errors,
How can I fix it?
Try
File file = new File("/mnt/sdcard/img.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview1.setImageBitmap(bitmap);
Related
I would like to set background in LinearLayout from file, path is correct, but not works
Bitmap bitmap5 = BitmapFactory.decodeFile(data.getData().getPath());
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap5);
mDrawingPad.setBackground(bitmapDrawable);
I have created the following navigation drawer layout but now I want to apply the sd card image on profile picture view. How can I apply it?
ScreenShot:
1) Read your image from the sd card:
File image = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "path_to_your_file");
if(image.exists())
Bitmap bmp = BitmapFactory.decodeFile(image.getAbsolutePath());
2) Set it into your appropriate ImageView
ImageView imgProfile = (ImageView) findViewById(R.id.your_image_view_id);
imgProfile.setImageBitmap(bmp);
Getting Image from sdcard
Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageView img;
img.setImageBitmap(bmp);
//add permission AndroidManifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
I get url to my Image, and want it make Image;
I want use this methods
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit().createImage(url);
or
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
Image image = ImageIO.read(url);
but Android Studio can't find ImageIO and java.awt.Toolkit. How can I add them?
You can get Bitmap from url and then you can use that bitmap further to set image of an ImageView.
URL url = new URL("http://....");
Bitmap image=BitmapFactory.decodeStream(url.openConnection().getInputStream());
I'm trying to load a png image as a drawable from my device sd card.
I use the following function but it doesn't work:
public Drawable getDrawable()
{
return new BitmapDrawable(imagePath);
}
The image path is: mnt/sdcard/MyFolder/image.png
The app crashes when I try calling that method, how should I load my png image located in my sdcard and cast it into a Drawable object?
There is actually a BitmapDrawable constructor straight from file path. The method you are using is depricated. Try:
Drawable myDrawable = new BitmapDrawable(getResources(), pathName);
If this doesnt work, Try getting a bitmap and creating a drawable from it:
The bitmap can be created with decodeFile
You can use it like this:
Bitmap myBitmap = BitmapFactory.decodeFile(pathName);
Then you can use the bitmap for drawing etc.
to convert Bitmap to drawable use
Drawable myDrawable = new BitmapDrawable(getResources(), myBitmap);
Take a look Here (Bitmaps) and Here (Bitmap Drawables) for more info.
I am simple do like that
public Drawable getDrawableFromPath(String filePath) {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
//Here you can make logic for decode bitmap for ignore oom error.
return new BitmapDrawable(bitmap);
}
I've been trying to make a small gallery and the bitmaps are always returning null. The code is like this:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
//Toast.makeText(getApplicationContext(),imgArray2.length+ " Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
//Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position]);
//Uri uri = Uri.parse(imgArray2[position]);
//Bitmap bitmap = decodeFile(new File(uri.toString()).getAbsoluteFile());
//Bitmap bitmap = BitmapFactory.decodeFile(uri.toString());
//int imgID = getResources().getIdentifier(path, "drawable", "mypack.pack");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 15;
Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position], options);
//i.setImageResource(imgArray2[position]);
i.setImageBitmap(bitmap);
//Uri uri = Uri.parse(imgArray2[position]);
Toast.makeText(getApplicationContext(), "Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
//i.setImageURI(Uri.parse(imgArray2[position]));
i.setLayoutParams(new Gallery.LayoutParams(170, 170));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
As you can see by the commented out code, I've been trying many option. setImageURI works but I need to scale down the image as I have many. The images are in the sd card. I checked the path of image and it is correct. What am doing wrong here?
Did you analyze the logs? Sometimes when images are too big to decode, the Dalvik VM will deny a memory request resulting in a null image being returned.