Can't setImageURI(). Why? - android

I want to add images to the ViewHolder. I do everything in method onBIndViewHolder(). I get String variable path from db, try to parse it by Uri and setImageUri, but it doesn't work. For debuggind I log path to the console.
String imgPath = images.get(position);
Log.d("imgTag", imgPath);
Uri imageURI = Uri.parse(imgPath);
//holder.image.setImageURI(imageURI);
Path of the image from log:
/document/image:46277
If I set image by setImageResource() it works fine, but I need from URI.

Related

I have the bitmap of an ImageView and I want to get the path of it uisng MediaStore API without using .insertImage()?

binding.btnShareOne.setOnClickListener {
val intent = Intent(Intent.ACTION_SEND).setType("image/*")
val bitmapDrawable = binding.ivLayoutOne.drawable.toBitmap()
val path = MediaStore.Images.Media.insertImage(
contentResolver,
bitmapDrawable,
"tempimage",
null
)
val uri = Uri.parse(path)
intent.putExtra(Intent.EXTRA_STREAM, uri)
startActivity(Intent.createChooser(intent, "Share Image"))
}
This is my code. I want to get the path from the bitmap and pass it to Uri. When I run my current code it gives me " java.lang.NullPointerException: uriString " error.
I have researched a bit and I think it will be solved by scoped storage but I cannot seem to implement it.
Since .insertImage() is deprecated and also Uri is returning null, this method of mine is not working to get image from image view and share it.
Please help.
A bitmap object doesn't have a path. A Bitmap object is an in memory object. Its just raw bytes. Even if it came from a file, that association isn't kept around.

Is there any way to create Uri from bitmap but i don't want to see it in image gallery?

Scenario: I am using MessagingStyle notification in a chat application. For image messages, I am using setData function to create image notification in conversation style.
public Message setData(String dataMimeType, Uri dataUri)
This method required dataUri but I have URL of image so I have created a bitmap from image url and then create a uri from batmap using function below.
public static Uri getImageUri(Context applicationContext, Bitmap photo) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(), photo, "IMG_" + Calendar.getInstance().getTime(), null);
if (path == null) {
return null;
}
return Uri.parse(path);
} catch (SecurityException ex) {
ex.printStackTrace();
return null;
}
}
Problem: Now the problem is that when user open image gallery, these notifications images are showing because I used mediastore to create URI. I don’t want these image to show in gallery.
Limitation: I am also saving that URI in case If I have to generate this notification again. So I can’t delete that image immediately to avoid showing in gallery.
Question: Is there any way to save image privately in external storage where gallery don’t have access. But notification manager can access?
Is there any way to create Uri form url without downloading it? I have tried this but it also not working
url = new URL(messageNotification.getAttachmentImage());
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
imageUri= Uri.parse(uri.toString());
Another workaround to show image in notification by using Messagings style like conversation as in WhatsApp
Download your image directly from url to a file in getExternalFilesDir(). Dont use an intermediate bitmap.
The MediaStore has no access to your getExternalFilesDir() so Gallery apps dont know about your files there.
The user can still use the Files app to see those images.
Then use a FileProvider to serve your files. Construct an uri with FileProvider.getUriForFile().
store your bitmap as File in some app-specific directory (not gallery) and make URI from file path. be aware of Scoped Storage and pick proper place for storing Bitmaps - check out in HERE, probably getFilesDir()/getCacheDir() method will be the best option
also worth trying may be this line:
Uri uri = Uri.parse(messageNotification.getAttachmentImage());
(encodedImageUrl = URLEncoder.encode(imageUrl, "utf-8") may be needed)

How to Load an image in an activity in android.

This code save the image in " MyApp ".but how to load the image in other activity
final EditText txtRegid = (EditText)this.findViewById(R.id.regid);
String RegID = txtRegid.getText().toString();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mImageFile = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",
"PIC"+RegID+".jpg");
String mSelectedImagePath = mImageFile.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mImageFile));
startActivityForResult(intent, TAKE_PICTURE);
Well, if you have defined the BitMap variable as a data member of your MyApp class, you can easily fetch it in other activities by using a getter() function to fetch that BitMap image which you are storing in MyApp.
Or, you could pass your image along with Intent, by encoding it into String format.
If you know the absolute path of the file. You can use BitmapFactory.decodeFile(filePath, opts) method to get the Bitmap. Then use ImageView's method setImageBitmap() to show it.

How to load thumbnail with Universal Image Loader for Android?

I am using this lib:
https://github.com/nostra13/Android-Universal-Image-Loader
I want to load an image thumbnail into an ImageView using the lib.
What content uri can I pass into:
imageLoader.displayImage(imageUri, imageView); //?
The docs give an example of using a content url:
String imageUri = "content://media/external/audio/albumart/13"; // from content provider
I pretty much want to switch from doing this:
return MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
to using the lib.
As you can see I have the id but I need to construct a url for MICRO_KIND image thumbnails...
I think you can use next URI format: content://media/external/images/thumbnails/13
String uri = "content://media/external/images/thumbnails/" + id;

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