Reputation: 63
I am trying to load image from file path in local storage to an imageView.
private final String PATH = "file:///storage/emulated/0/Downloads/image.png"
As suggested elsewhere in the forum I've tried to load it both using Glide:
Glide.with(this)
.load(PATH)
.into(imageView);
and Bitmap/BitmapFactory:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
File f = new File(PATH);
if(f.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
but I always get the placeholder image.
In the manifest I added the permission WRITE.EXTERNAL.STORAGE and READ.EXTERNAL.STORAGE.
Trying to debug it seems to load the correct path in f variable.
Thanks for help
Edit: I have tried all the suggested solution, but I continue gettin blank screen...
I have also made a brand new app with one imageView only to better test:
package com.example.android.pic;
import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import java.io.File;
public class MainActivity extends AppCompatActivity {
Uri PATH = Uri.fromFile(new File((Environment.getExternalStorageDirectory().getPath() + "/Pictures/agata.png")));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
ImageView imageView = (ImageView) findViewById(R.id.pic);
Glide.with(this)
.load(PATH)
.into(imageView);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
ImageView imageView = (ImageView) findViewById(R.id.pic);
Glide.with(this)
.load(PATH)
.into(imageView);
} else {
finish();
}
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
please help! Thanks
Upvotes: 4
Views: 11124
Reputation: 344
You Should use this code
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(selectedImage);
in this code you want imageUri that can use StringPATH:
private final StringPATH="/storage/emulated/0/Downloads/image.png"
Upvotes: 1
Reputation: 173
Your file path deceleration is wrong. Try changing PATH to
private final StringPATH="/storage/emulated/0/Downloads/image.png"
Upvotes: 0