I am trying to get image that inside drawable folder by path
so i try this but not working
String path = "android.resource:///" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk";
File imgFile = new File(path);
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageBitmap(myBitmap);
}
I found this simplest solution
Uri path = Uri.parse("android.resource://" + BuildConfig.APPLICATION_ID + "/drawable/logo_ataturk");
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageURI(path);
Resources are not files on the device. They are files on your developer machine, nothing more.
You can replace all of this code with:
ImageView imgLogo = findViewById(R.id.imageView_logo);
imgLogo.setImageResource(R.drawable.logo_ataturk);
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier($name_of_the_image, "drawable", getPackageName()));
or
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_icon);
imgLogo.setImageBitmap(myBitmap);
Related
I want to get Bitmap from SimpleDraweeView of Fresco lib, and save it to SD Card.
SimpleDraweeView is child class of ImageView so it should support getDrawable() but calling this method throws ClassCastException
BitmapDrawable bitmapDrawable = (BitmapDrawable) viewHolder.drawee.getDrawable();
ClassCastException: com.facebook.drawee.generic.RootDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
What is the wayout for this?
s1rius's answer mentioned by Morrison helped me. Here's the code that I used.
ImageRequest downloadRequest = ImageRequest.fromUri(uri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(downloadRequest, context);
if (ImagePipelineFactory.getInstance().getMainFileCache().hasKey(cacheKey)) {
BinaryResource resource = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey);
File cacheFile = ((FileBinaryResource) resource).getFile();
File savedImageFile = new File(Constants.APP_PATH_SAVED_QUOTES, "M1iS1" + "_" + currentPosition + ".jpg");
}
Then I copied cacheFile into savedImageFile.
I'm trying to create a thumbnail from a video and place it in an ImageView in Android, but it doesn't work. I'm not sure if the file path I use is correct.
The code I'm using:
ImageView iv = (ImageView) findViewById(R.id.imageView1);
String filepath = "android.resource://" + getPackageName() + "/" + R.raw.videotest;
Bitmap bm = ThumbnailUtils.createVideoThumbnail(filepath, Thumbnails.MINI_KIND);
iv.setImageBitmap(bm);
I look for a solution to replace
Images. Media. EXTERNAL_CONTENT_URI (type String uriString) by my asset
InputStream ims = getAssets().open("a.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
Bitmap bitmap=drawableToBitmap(d);
Uri imageUri = Uri.parse( Images.Media.EXTERNAL_CONTENT_URI + "/" );
bitmap = ImageLoader.loadFromUri( this, imageUri.toString(), 1024, 1024 );
mImageView.setImageBitmapReset( bitmap,0,true);
thank
You can try this:
File file = new File("path_to_image_file");
Uri imageUri = Uri.fromFile(file);
I am trying to get the image from sdcard to display it in an imageview in android. Its showing error as SkImageDecoder:factory returned null in android emulator2.2. Here is my code to retrieve image from SDcard to imageview in android.
File imgFile = new File("/sdcard/wm.png");
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView1);
myImage.setImageBitmap(myBitmap);
}
Please resolve this issue.
Use Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory instead of hard coding the path of the sd card (i.e "/sdcard/wm.png").
I am trying to display a .jpg from my sd card into an imageview in my layout. I dont get any errors, but nothing gets displayed. I would appreciate any help. Thanks
EDIT:
layout1 = (LinearLayout) findViewById(R.id.layout1Activity);
String imgFile = Environment.getExternalStorageDirectory() + "/Apple.jpg";
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile);
ImageView myImage = new ImageView(this);
myImage.setImageBitmap(myBitmap);
layout1.addView(myImage);
do NOT access the SD card directly, try accessing it trough Environment. Like this:
String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";
and then you can call BitmapFactory:
Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);