Using external image as drawable resource - java

So I have an image saved on the external SD-Card. Now, I want to use that image as a resource in my project. (I want to use the image located at the specific path instead of R.drawable.image).
It would be also great, if there was a way to kinda "add" the image to the resources (and not just use it), that the image gets something like a resourceID like the ones in R.drawable got.
But basically I just want to display an external image in an imageView.

First you will need read external storage permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
then,
String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() +"directoryName/imageName.png";
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
ImageView imageView = (ImageView)findViewById(R.id.imageview);
imageView.setImageBitmap(bitmap);

Use the following code.
File file = new File("put your bitmap address here");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

You cannot add any files to the app package (apk file) in runtime. If you want the image in your drawable folder, you should add it before packaging.
String path = "path-to-image";
Bitmap bitmap = BitmapFactory.decodeFile(path);
Drawable drawable = new BitmapDrawable(bitmap); //if you want a drawable

Related

How to save bitmap in new folder in DCIM to show it in gallery on cellphone (the cellphone doesn't have SDCard)?

I have an URL, I download the image in it and convert it to bitmap. I want to download the bitmap in the internal memory of the cellphone to be in the gallery. I found solutions but includes SDcard and I need to save the image even if the cellphone doesn't have external memory. How can I achieve that?
Well not all cam app store on DCIM but you can
MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap,
System.currentTimeMillis() + ".jpg", "DESCRIPTION HERE");

how to get image path from resource as a string in java in android

I want the image path from the drawable under resources as a string but it shows error . But I can't identify what the error is. I provide some code snippet.
listView = view.findViewById(R.id.listView);
ArrayList<Card> list = new ArrayList<>();
//here the error
String img = "drawable://" + R.drawable.tiger;
Note: when I placed image name (tiger) it gives me error
list.add(new Card("drawable://" + R.drawable.tiger, "Royal Bengal Tiger"));
i want the image path from the drawable under resources
A drawable resource is a file on your development machine. It is not a file on the device. There is no path to it on the device.
i provide some code snippet
There is no drawable scheme in Android. This also is not an image path.
If your Card requires a Uri, you can try android.resource as a scheme.
That's not how you get resources
what you want is ContextCompat.getDrawable(context, R.drawable.name);

Convert Drawable path to URI

I have an image in my #drawable/myimage.jpg of which I would like to load on my ImageView through Glide. Glide however, only accepts Uri's to load the actual image to the ImageView.
Here's the code:
uriImage = Uri.parse("#drawable/myimage.jpg");
//LOADS IMAGES USING GLIDE
Glide.with(AboutActivity.this).load(uriImage).into(imgView);
It somewhat doesn't work. Is there any work-arounds this?
Sorry for being naive, I just started using Android Studio again :)
try this code:
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
it helps you
Simply do this!
Glide.with(AboutActivity.this).load(R.drawable.resouce_name).into(imgView);

Storing Image Thumbnail INTO Image in Android

I am implementing a gallery of images that are stored in a folder in the SD card. The images are copied from the server. I am trying to display thumbnails using the Interface ExifInterface. It has a very nice method getThumbnail. This method returns a thumbnail if it exists or null if not. But the condition is that the image NEEDS to contain the thumbnail WITHIN.
My Question is: How can I create a thumbnail INSIDE the image when I save the image to SD card so that ExifInterface.getThumbnail detects that thumb and does not return null
You can create your own thumbnail with ThumbnailUtils class (added in API level 8).
Bitmap source = BitmapFactory.decodeFile(pathToImage);
Bitmap thumb = ThumbnailUtils.extractThumbnail(source, thumbWidth, thumbHeight);
ExifInterface class is very limited, so you cannot attach thumbnails.

Reading an Image file from External Storage and placing in an ImageView

I've a situation in which I need to populate an image view with an image from the user's external storage directory; I've been decoding a file using a Bitmap Factory and then setting the image view bitmap accordingly, but I keep running into Null Pointer Exceptions.
String path = Environment.getExternalStorageDirectory()+ "/Pictures/test.jpg";
File imgFile = new File(path);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView imgView = (ImageView)findViewById(R.id.imageView2);
imgView.setImageBitmap(myBitmap);
} else {
Log.d("Ciaren", "File doesn't exist");
}
I'm running this directly in OnCreate, and the image view should be full screen, I can't for the life of me figure out what is throwing a null object though, as I've stepped through and all seems fine, the null pointer is thrown when the setImageBitmap(); method is called.
The error is thrown in imgView.setImageBitmap(myBitmap); and thus imageView is null. As a result your imageView is not found. Make sure the resource id R.id.imageView2 is correct. Sometimes there are problems with eclipse and a Project > Clean can fix that too.

Categories

Resources