-1

I have tried many different ways or solutions and none of them seems to work. I have tried a few specific solutions which returns me a string and a context but i do not know what to do with the context even if I set the receiver of the context as null the app returns an error. What I want to do is, to be able to upload an image file for that I need the file path, a Uri or a content Uri gives me addresses like this

Content://something_something/304:

But i need something like this " storage/sdcard/something_something/304.jpg"

how do it get that in KITKAT?

This is the code/method that I use for getting the path to the selected user Image.

public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

hope i provided enough details. Thank you for any help.

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Assassin Shadow
  • 61
  • 1
  • 2
  • 9
  • possible duplicate of [Android Gallery on KitKat returns different Uri for Intent.ACTION\_GET\_CONTENT](http://stackoverflow.com/questions/19834842/android-gallery-on-kitkat-returns-different-uri-for-intent-action-get-content) – Ken Y-N Sep 02 '14 at 04:26
  • follow this link http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0 and attach your log cat snapshots – Spry Techies Sep 02 '14 at 04:29

1 Answers1

0

KITKAT does not provide the actual image path for security purpose.

if (Build.VERSION.SDK_INT >19){
    InputStream imInputStream =  getContentResolver().openInputStream(data.getData());
    Bitmap bitmap = BitmapFactory.decodeStream(imInputStream);
    String imagePath = saveGalaryImageOnKitkat(bitmap);
}
    ///After use you can delete the bitmap.



    private String saveGalaryImageOnKitkat(Bitmap bitmap){
            try{
                File cacheDir;

                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                cacheDir=new File(Environment.getExternalStorageDirectory(),getResources().getString(R.string.app_name));
                else
                cacheDir=getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                if(!cacheDir.exists())
                cacheDir.mkdirs();

                String filename=java.lang.System.currentTimeMillis()+".png";
                File file = new File(cacheDir, filename);


                file.createNewFile();

                FileOutputStream out = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                return file.getAbsolutePath();
                }catch(Exception e){
                    e.printStackTrace();
                }

            return null;

        }
Dhruba Bose
  • 446
  • 2
  • 7