How to dynamically set a imageView from a file? - android

I need to set a imageView dynamically. The image is at drawable. This code is ok:
Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
img_pergunta.setImageBitmap(bm2);
The problem is that I need to start from string, but this code doesn´t work:
File imgFile = new File("/drawable/image1.png");
Bitmap bm2 = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
img_pergunta.setImageBitmap(bm2);
How do I solve it?

You can get the id (which you use in decodeResource()) using context.getResources().getIdentifier() and a name like image4.

You probably have given a bad path to the file, try
File imgFile = new File("android.resource://COM.EXAMPLE.PACKAGE/drawable/image1.png");
but replace the package name with correct one - yours

You can create new file by giving path as mentioned below,
File file = new File("android.resource://"+R.class.getPackage().getName()+"/" +resourceId);
resourceId can be replaced by R.drawable.resourceName

Related

Don't show image in ImageView

I have in my phone two folders Card and Phone. I create folder MyPhone in the folder Card and added in her an image flap.png. This image I want to show in the element ImageView. I want to use this variant. What am I doing wrong? Thank you.
button1.setOnClickListener {
var intent: Intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.parse("/mnt/sdcard/MyPhoto/flap.png"),"image/*")
imageView1.setImageURI(intent.data)
}
if you want only this image you can use below code
File imgFile = new File("/mnt/sdcard/MyPhoto/flap.png");
if(imgFile.exists()){
Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView1.setImageBitmap(bm);
}else{
Log.e("tag","FILE NOT EXIST")
}

Displaying from external storage?

I have an image saved in my Pictures folder, how to display it in a imageview?
like:
imageview.setimage("//Pictures//cat.jpg)
I know it's not a correct code, but I want to achieve something like this, hope someone can help, thanks!
you first generate a bitmap from file path and then put that bitmap to imageview
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
Edit: Also add this permission in your manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You can set image like this from the sd card get path and create file variable and decode file using BitmapFactory set imageview image
String path = Environment.getExternalStorageState()+"/Pictures//cat.jpg";
File f = new File(path);
imageview.setImageBitmap(new BitmapFactory.decodeFile(f.getAbsolutePath()));
First you passing wrong file path.
for Correct File path do like this
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());
then create your URL like.
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath()) + "/cat.jpg";
Then use like this.
File image = new File(filePath);
imageView.setImageBitmap(new BitmapFactory.decodeFile(image.getAbsolutePath()));
Got it working with combination of the 2 posted codes, thanks for everyone, here's the working code:
Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath();
String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg";
File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
image1.setImageBitmap(bitmap);

FreeImage problems on Android (NDK)

I tried to use FreeImage library to load PNG as a texture (from memory). That's the fragment of code:
FIMEMORY *fiStream = FreeImage_OpenMemory(streamData, size);
FREE_IMAGE_FORMAT fileFormat = FreeImage_GetFileTypeFromMemory(fiStream, 0);
FIBITMAP *image = FreeImage_LoadFromMemory(fileFormat, fiStream, 0);
int bitsPerPixel = FreeImage_GetBPP(image);
width = (int)FreeImage_GetWidth(image);
height = (int)FreeImage_GetHeight(image);
I'm using FILE with fopen to open file and then read stream to streamData object. File and stream is read correctly.
The result is: fileFormat = -1 and image is NULL.
I also tried to use FreeImage to load PNG file directly from disk using FreeImage_Load, but the result is the same - it returns NULL.
Has anybody faced similar problem? Can you suggest an alternative to FreeImage that can read data from memory?
try this code to load your image from memory:
File file = new File("/sdcard/Images/image1.jpg");
if(file.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView image = (ImageView) findViewById(R.id.imageview);
image.setImageBitmap(bitmap);
}

Android: Displaying .jpg image from sdcard using ImageView

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);

Android get image path from drawable as string

Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.
Any ideas how to do that?
These all are ways:
String imageUri = "drawable://" + R.drawable.image;
Other ways I tested
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
String path = path.toString();
String path = otherPath .toString();
based on the some of above replies i improvised it a bit
create this method and call it by passing your resource
Reusable Method
public String getURLForResource (int resourceId) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}
Sample call
getURLForResource(R.drawable.personIcon)
complete example of loading image
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
you can move the function getURLForResource to a Util file and make it static so it can be reused
If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.
InputStream stream = getAssets().open("image.png");
Drawable d = Drawable.createFromStream(stream, null);
I think you cannot get it as String but you can get it as int by get resource id:
int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder
Sample Code
File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
if(!imageFile.exists()){//file doesnot exist in SDCard
imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
}

Categories

Resources