Image not found /Documents/Image:XXXX - android

I'm selecting an image from the gallery selector after pressing a button, and then filling a RecyclerView with these images. However, it seems the pictures I'm selecting cannot be found...
To open the gallery selector I do:
ActivityResultLauncher<String> GetImageFromGallery = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
#Override
public void onActivityResult(#Nullable Uri uri) {
if (uri != null) {
ArrayList<String> temp = new ArrayList<>(0);
temp.add(uri.getPath());
img_data.add(temp);
mAdapter.notifyDataSetChanged();
}
}
});
// Button
Button btn_add = root.findViewById(R.id.btn_add_image);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
GetImageFromGallery.launch("image/*");
}
});
where img_data is an `ArrayList<ArrayList> and mAdapter is the adapter of a RecyclerView. Inside the RecyclerView I'm reading the images to show in an ImageViewer as follows:
try {
viewHolder.getphoto().setImageBitmap(BitmapFactory.decodeStream(viewHolder.getphoto().getContext().getApplicationContext().getContentResolver().openInputStream(Uri.parse(localDataSet.get(position).get(0)))));
//Or viewHolder.getphoto().setImageBitmap(BitmapFactory.decodeFile(localDataSet.get(position).get(0)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
However, none of above works. I'm getting stuck with a not found file error. Path are always of style /Documents/Image:XXXX where X are numbers. What am I doing wrong?

Following the code in my question, use this to add the uri as a String:
temp.add(uri.toString());
and this to load the image:
viewHolder.getphoto().setImageURI(Uri.parse(localDataSet.get(position).get(0)));
Although if possible use Glide to load the image. Without Glide was extremely slow, but with it it wasn't. To add glide add the following dependencies intro build.gradle module:
implementation 'com.github.bumptech.glide:glide:4.12.0'
// Glide v4 uses this new annotation processor -- see https://bumptech.github.io/glide/doc/generatedapi.html
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
and then to load the picture:
Glide.with(viewHolder.getphoto().getContext())
.load(Uri.parse(localDataSet.get(position).get(0)))
.into(viewHolder.getphoto());

Related

Is there a better way to load a picture in android studio?

I am making an app using Java in android studio, and in it I want users to have the option to set a custom profile picture. I'm using firebase storage to store the images for the PFPs, and taking them from there every time I load it, but it always takes around half a second or so to load the profile picture: gif of the problem
Here's my code for loading the pfp in the homepage (I am using de.hdodenhof.circleimageview.CircleImageView to make the ImageView circular):
public class HomePage extends AppCompatActivity {
de.hdodenhof.circleimageview.CircleImageView pfpImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
pfpImage = findViewById(R.id.pfpImg);
Intent intent = getIntent();
if (intent != null && intent.hasExtra("UserEmail")) {
String userEmail = intent.getExtras().getString("UserEmail");
StorageReference userPfp = DBRef.refStorage.child("ProfileImages/Users/" + userEmail.replace(".",",")); //DBRef is a class I created to reference all Firebase related objects.
userPfp.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.with(HomePage.this).load(uri).into(pfpImage); // I am using com.squareup.picasso:picasso:2.5.2 to load the image from the Uri.
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(HomePage.this, "Profile Picture Loading Failed", Toast.LENGTH_SHORT).show();
}
});
}
else
pfpImage.setImageResource(R.drawable.doggo);
}
}
Is there some better way to load/store the pictures so they appear when the page loads?
EDIT: I managed to negate this by not loading from Firebase every time the activity is opened, but only the first time and saving it, using an idea from this post
There is other library named glide that can load picture faster when the page loads.
Here is the link for the reference.
https://guides.codepath.com/android/Displaying-Images-with-the-Glide-Library

Unable to download image using Picasso on Android

I'm trying to download the following image using Picasso https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg
I can see it fine in a browser. However when I attempt to download it using Picasso, I get an error (a breakpoint in my onError() method gets hit).
This is an extract of my code:
final RequestCreator rc = with(context).load(fullImagePath);
if (fit != null && fit) {
rc.fit();
}
// If no callback listener exists, create one.
if (callbackListener == null) {
callbackListener = new Callback() {
#Override
public void onSuccess() {
L.p("onSuccess retrieving " + fullImagePath);
}
#Override
public void onError() {
// Something went wrong
L.p("Error retrieving " + fullImagePath);
}
};
}
rc.into(fImageView, callbackListener);
I found this: https://github.com/square/picasso/issues/500 however it's a bit dated and the OkHttpClient class no longer has the setProtocols() method.
Thanks!
Inserting that link as "http" instead of "https" will work.
The below code works for me for both http and https links.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
String https = "https://s3-media4.fl.yelpcdn.com/bphoto/94E7Ti0RTDbA6mGotZw5DA/o.jpg";
String http = "http://www.sunseed.org.uk/projectpack/files/2014/05/Library_1400_800-1200x686.jpg";
Picasso.with(this)
.load(https)
.placeholder(R.drawable.me)
.into(imageView);
}
Also be sure you write the below permission in manifest file:
<uses-permission android:name="android.permission.INTERNET" />
also write the following in build.gradle file
implementation 'com.squareup.picasso:picasso:2.5.2'

Fresco Image Viewer doesn't show photo

I'm using fresco image viewer on my app for showing post photos when clicked make it full screen. It was working last week, i didn't changed anything in my code but now it doesn't work.
Here is my showFullScreen function
public void showFullScreenImage(final Context activity, final String imageURL) {
Handler handler = new Handler(activity.getMainLooper());
handler.post(new Runnable() {
public void run() {
try {
List<String> pictures = new ArrayList<>();
pictures.add(imageURL);
GenericDraweeHierarchyBuilder hierarchyBuilder = GenericDraweeHierarchyBuilder.newInstance(mContext.getResources())
.setFailureImage(R.drawable.error)
.setProgressBarImage(new ProgressBarDrawable());
new ImageViewer.Builder<>(activity, pictures).setCustomDraweeHierarchyBuilder(hierarchyBuilder).setStartPosition(0).show();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
It shows black screen, but it's not freezing, i can swipe to dismiss the view.
Okay I found the solution, upgrade all repos the final update from build.gradle fresco and frescoImageViwer than it'll be work.

Image are not displaying from Cache memory using Picasso Library

I am using Picasso library for image download and displaying it in imageView. This library also store images in cache and memory. When my internet is turn on, i am able to view images on imageView. So i think, it should also be store in cache or file memory. Now my internet is turnOFF, but it doest not display to images. kindly have a look.
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError(){
holder.loadingBar.setVisibility(View.VISIBLE);
Picasso.with(context)
.load(url) .placeholder(R.drawable.defaultimg)
.into(holder.imageview2, new ImageLoadedCallback(holder.loadingBar) {
#Override
public void onSuccess() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
#Override
public void onError() {
if (holder.loadingBar != null) {
holder.loadingBar.setVisibility(View.GONE);
}
}
});
}
});
Finally I resolved that issue. Thanks #dev.bmax
image url was not correct. There is bug in Picasso. If we have url like as
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg?500|700
Picasso is able to displaying image when internet TURN ON
but if we TURN OFF to internet, it does not decode the url. and not display the image also.
We have to remove ?500|700 then i was able to view image in OFFLine mode also.
//url.substring(0,url.indexOf("?"))
https://i.ytimg.com/vi/DMVEcfQmPOs/maxresdefault.jpg
Thanks!

Fresco: Use current image displayed in Drawee as a placeholder for next request

I play multiple images sequentially on the same SimpleDraweeView, the issue is that when submitting a new imageURI request, theSimpleDrweeView will remove the current displayed image and replace it with nothing until the URI is downloaded. So it will leave gaps in the playing sequence ( you could think of what I'm trying to do is cartoon animation using local photos). What I would like for the SimpleDrweeView to leave the current image as is until the new one is downloaded and then just swap it when it's ready.
I tried using the low-res/high-res scheme from this ticket to put the old uri as a placeholder but that didn't work (had the same effect as before).
This is what I have now:
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
draweeView.setImageURI(uri /* local image */);
And this is what I tried so far (didn't work):
SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
Uri lowResUri, highResUri;
DraweeController controller = Fresco.newDraweeControllerBuilder().setTapToRetryEnabled(true)
.setLowResImageRequest(ImageRequest.fromUri((Uri) draweeView.getTag())) /*naive way to test the low/high res feature*/
.setImageRequest(ImageRequest.fromUri(uri))
.setOldController(draweeView.getController())
.build();
draweeView.setTag(uri);
draweeView.setController(controller);
I am part of the Fresco team and may be able to help.
It is strange that you experience the same issue with low-res/high-res combination. If the image is currently displayed, it means it should be in the bitmap memory cache, which in turn means that it should be able to load immediately when set as a low-res image the next time you are switching to the next frame.
Are you sure that you are setting the correct uri as low-res image? (Uri) draweeView.getTag() looks kind of suspicious. I would double check that part.
If the uri is indeed correct, but the image is not in the bitmap cache anymore, it would be worth investigating why the image that is visible is not cached anymore, as we have explicit logic in place that should prevent evicting visible images. See how to track this with verbose logging here.
If all of the above fails, third options is to actually implement your own DataSource. I can help with that, but this might be somewhat involving. The basic idea is to implement a DataSource that wraps another DataSource that actually provides the image. Then you could do something like this:
// keep this instance somewhere
mMyDataSourceSupplier = new MyDataSourceSupplier();
// build controller by specifying your custom datasource supplier instead of specifying any URIs.
Fresco.newDraweeControllerBuilder()
.setDataSourceSupplier(mMyDataSourceSupplier)
.build()
// later, when you need to change the image do
mMyDataSourceSupplier.setUri(nextUri);
// this is just an outline
class MyDataSourceSupplier implements Supplier<DataSource<CloseableReference<CloseableImage>>> {
private Uri mCurrentUri;
private DataSource<CloseableReference<CloseableImage>> mCurrentDataSource;
public void setUri(Uri uri) {
mCurrentUri = uri;
if (mCurrentDatasource != null) {
mCurrentDataSource.setUri(uri);
}
}
#Override
public DataSource<CloseableReference<CloseableImage>> get() {
mCurrentDataSource = new MyDataSource();
mCurrentDataSource.setUri(uri);
return mCurrentDataSource;
}
private class MyDataSource extends AbstractDataSource<CloseableReference<CloseableImage>> {
private DataSource mUnderlyingDataSource;
#Override
protected void closeResult(#Nullable CloseableReference<CloseableImage> result) {
CloseableReference.closeSafely(result);
}
#Override
#Nullable
public CloseableReference<CloseableImage> getResult() {
return CloseableReference.cloneOrNull(super.getResult());
}
#Override
public boolean close() {
if (mUnderlyingDataSource != null) {
mUnderlyingDataSource.close();
mUnderlyingDataSource = null;
}
return super.close();
}
public void setUri(Uri uri) {
if (mUnderlyingDataSource != null) {
mUnderlyingDataSource.close();
mUnderlyingDataSource = null;
}
if (uri != null && !isClosed()) {
mUnderlyingDataSource = Fresco.getImagePipeline().fetchDecodedImage(ImageRequest.fromUri(uri), null);
mUnderlyingDataSource.subscribe(new BaseDataSubscriber {
#Override
protected void onNewResultImpl(DataSource<List<CloseableReference<CloseableImage>>> dataSource) {
MyDataSource.super.setResult(dataSource.getResult(), false);
}
});
}
}
}
}

Categories

Resources