0

There is a function that records the image on the SD card

String folderToSave = Environment.getExternalStorageDirectory().toString();

private String SavePicture(ImageView iv, String folderToSave)
{
    OutputStream fOut = null;

    try {
        File file = new File(folderToSave, mainText.getText()+".jpg"); 
        fOut = new FileOutputStream(file);

        Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
        fOut.flush();
        fOut.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(),  file.getName());
    }
    catch (Exception e) 
    {
        return e.getMessage();
    }
    return "";
}

The function code is taken from Habr

I'm trying to set an ImageView via setImageDrawable but I'm getting an error

mainText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Drawable d = Drawable.createFromPath(folderToSave+mainText.getText()+".jpg");
        ImageView.setImageDrawable(d);
    }
});

Error

non-static method setImageDrawable(Drawable) cannot be referenced from a static context               ImageView.setImageDrawable(Drawable.createFromPath(folderToSave+mainText.getText()+".jpg"));

Is there any other way to set the picture saved on the phone?

  • https://stackoverflow.com/questions/3870638/how-to-use-setimageuri-on-android – blackapps Dec 03 '22 at 12:56
  • 1
    `Drawable.createFromPath()` is extremely obscure. For a decade, Android app developers have focused on using image-loading libraries, such as [Glide](https://bumptech.github.io/glide/) and [Picasso](https://github.com/square/picasso). They can handle background threading, image scaling, and other concerns that you are presently ignoring. Your bigger problem, though, is that you are trying to call `setImageDrawable()` on a *class*, rather than on an instance of `ImageView` from your layout. – CommonsWare Dec 03 '22 at 14:39
  • How are you able to call `setImageDrawable(Drawable)` from the class? Its a non-static method! – Sambhav Khandelwal Dec 03 '22 at 15:20

0 Answers0