Don't show image in ImageView - android

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")
}

Related

How to dynamically set a imageView from a file?

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

Displaying image from gallery in Oncreate method without opening gallery intent

I know how to open gallery intent and to display in the imageview.
But I am facing problem in selecting image automatically in oncreate method and display in the imageview.
I have an image in gallery folder with name "myfile.jpg"
Can anyone guide me how to do it without openting gallery intent.
Thank you in advance
Here is solution :
ImageView image = (ImageView)findViewById(R.id.myImage);
File root = Environment.getExternalStorageDirectory();
String path = root.getAbsolutePath();
path = path + File.separator + "myfile.jpg"; //where you image file located
Bitmap myBitmap = BitmapFactory.decodeFile(path);
image.setImageBitmap(myBitmap);

Select a picture from asset folders in Android

I want the user to be able to select a picture from a given set of pictures that come with my app.
Is it possible to use new Intent(Intent.ACTION_PICK) but not with media files on the phone of the user, but with some pictures/bitmaps I defined earlier? So far I only found examples to pick images from the gallery.
No, sorry, there is no ACTION_PICK that will pick images from your project's assets/ folder. You would need to create your own UI for that.
No.Intent(Intent.ACTION_PICK) choose picture from Gallery in your phone.
private static final int SELECT_PHOTOS = 100;
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTOS);
For asset folder Check this
try
{
// get input stream
InputStream inputStream = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(inputStream, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex)
{
return;
}

SkImageDecoder:factory returned null

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").

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

Categories

Resources