1

When I run the following code it gets run on android version 4.4 but it does not run on android version 6.0.1. On this version I get a blank screen. I have gone through the similar questions but my issue is still there. Please help me out. Thank you.

public class MainActivity extends AppCompatActivity {
private static final String TAG = "Error" ;
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();
File[] listFile;
private ArrayList<File> fileList = new ArrayList<File>();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showGalleryPreview();
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);


  }


     private void showGalleryPreview() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_READ_STORAGE);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                getFromSdcard();

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}


public void getFromSdcard()
{

    File file=  new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath());
    getfile(file);



 }


public ArrayList<File> getfile(File dir) {
    File listFile[] = dir.listFiles();
    if (listFile != null && listFile.length > 0) {
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].isDirectory()) {
                fileList.add(listFile[i]);
                getfile(listFile[i]);

            } else {
                if (listFile[i].getName().endsWith(".png")
                        || listFile[i].getName().endsWith(".jpg")
                        || listFile[i].getName().endsWith(".jpeg")
                        || listFile[i].getName().endsWith(".gif"))

                {
                    fileList.add(listFile[i]);
                }
            }

        }
    }
    return fileList;
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return fileList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        Bitmap myBitmap = BitmapFactory.decodeFile(String.valueOf(fileList.get(position)));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;

    }}

Here is the manifest file

    <?xml version="1.0" encoding="utf-8"?>
   <manifest   xmlns:android="http://schemas.android.com/apk/res/android"
package="r.image">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Here is the gradle file

     apply plugin: 'com.android.application'

    android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "r.image"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
  }

 dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
}
Namrata Singh
  • 203
  • 2
  • 8

3 Answers3

1

Most likely you're forgetting to request permissions at runtime when accessing storage. Since Marshmallow, it's not enough to just declare the permissions you need in Manifest.

Egor
  • 39,695
  • 10
  • 113
  • 130
1

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. You have to ask for storage permissions first before calling this :

getFromSdcard();

You can read about how to request for permissions here : http://developer.android.com/training/permissions/requesting.html

Frosty
  • 500
  • 4
  • 14
0

It's all about new Runtime Permission on Android M. Please take a look at this article http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en

nuuneoi
  • 1,788
  • 1
  • 17
  • 14