-1

I am trying to set an image from the sdcard to an ImageView, but it does not work. What am I doing wrong? I am sure that the file exists.

Here is code for MainActivity :

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = findViewById(R.id.imageView);
        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/Naruto_newshot.png";
        File file = new File(filePath);
        if (file.exists())
            imageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
    }
}

Here is the code for the XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

The AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.mbo.testimage">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application>
        ...
    </application>

</manifest>

Finally the image 200 × 149 pixels :

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Moussa
  • 4,066
  • 7
  • 32
  • 49
  • 1
    You do not have read access to that location, most likely: https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it Also, please use an image-loading library like Glide or Picasso, rather than loading images on the main application thread. – CommonsWare Dec 22 '18 at 16:17

2 Answers2

1

are you add permission for write external storage

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){

 int permissionCheck = ContextCompat.checkSelfPermission(
        this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {      
        ActivityCompat.requestPermissions(this, new String[] 
 {
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 33);
} 

    }
Manu Garg
  • 186
  • 2
  • 13
0

Add this permission in your manifest :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

In addition on Android 6 and newer you need to call this to know if you have the permission

boolean permission = checkSelfPermission("android.permission.READ_EXTERNAL_STORAGE") == PackageManager.PERMISSION_GRANTED;

if not you need to call :

 String[] perms = {"android.permission.READ_EXTERNAL_STORAGE"};
 requestPermissions(perms, REQUEST_CODE);

and finally you need to override this Activity method that will be called after the user response:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {}
from56
  • 3,976
  • 2
  • 13
  • 23