0

Here is my On Click Listener:

@Override
public void onClick(View view) {
  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  intent.setType("image/jpeg");
  intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
  startActivityForResult(Intent.createChooser(intent, "Complete action using"), PHOTO_PICKER_REQUEST_CODE);
}

here is my OnActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == PHOTO_PICKER_REQUEST_CODE && resultCode == RESULT_OK) {
     Uri selectedImageUri = data.getData();
     File imageFile = new File(getRealPathFromURI(selectedImageUri));
     long length = imageFile.length();
     length = length / 1024;
     Log.d(TAG, "onActivityResult: ImageSize " + length +" Kb");
  }
}

ImageSize always returns 0 (zero).

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
EdgeDev
  • 2,376
  • 2
  • 20
  • 37

1 Answers1

0

You probably missing the permission in the manifest. You need to add the following permission in AndroidManifest.xml:

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

Or, if you're targetting Android 6.0 (API level 23) and above, you need to Requesting Permissions at Run Time

And please check your getRealPathFromURI() method. I've testing with this following code and it's work:

public String getRealPathFromURI(Uri contentUri) {
  String res = null;
  String[] proj = { MediaStore.Images.Media.DATA};
  Cursor cursor = getContentResolver().query(contentUri, proj, "", null, "");
  if (cursor.moveToFirst()) {
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    res = cursor.getString(column_index);
  }
  cursor.close();
  return res;
}

Or use the code from Convert content:// URI to actual path in Android 4.4

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96