Sahid rahutomo
Sahid rahutomo

Reputation: 90

How to display image in android?

I have a problem for displaying image, the problem is glide can't process image from storage file path(uri), Could anyone help me?

This is my Code

TextView browsefile, filename;
ImageView gambar;
int RequestFile = 2;
Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    browsefile = findViewById(R.id.browsefile);
    filename = findViewById(R.id.filename);
    gambar = findViewById(R.id.gambarfile);
    mContext = MainActivity.this;

    browsefile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startOptionBrowseFile();
        }
    });


}

private void startOptionBrowseFile() {
    Intent openFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    openFileIntent.setType("*/*");
    startActivityForResult(openFileIntent, RequestFile);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RequestFile && resultCode == RESULT_OK && data != null){
        Uri selectedFile = data.getData();


        File path = new File(selectedFile.getPath());

        path.mkdirs();
        Glide.with(mContext).load(path).into(gambar);
        
        filename.setText(selectedFile.getPath());


    }
}

Updated , i've found the solution, i try to use Bitmap and its work,

Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),selectedFile);
            Glide.with(mContext).load(bitmap).diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true)
                    .into(gambar);
        } catch (IOException e) {
            e.printStackTrace();
        }

and this is the error enter image description here

Upvotes: 0

Views: 85

Answers (2)

hosein moradi
hosein moradi

Reputation: 492

Glide.with(mContext)
.load(new File(selectedFile.getPath())) 
.into(gambar);

Upvotes: 0

try this:

Glide.with(mContext).load(selectedFile.getPath()).into(gambar);

Upvotes: 0

Related Questions