lundi 29 juin 2015

Image browse not working in 5.1 android device

Hello friends i want to browse my gallery image in my so below is my code

enter image description here

see above image i click on Photos application .

Button Click

protected void importImage() 
{
    Intent intent = new Intent(
            Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(
            Intent.createChooser(intent, "Select File"),
            currentRequestCode);

}

onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == currentRequestCode) 
        {
            if (data!=null) {
                openGalleryImage(data);
                saveImage(uri_outputFileUri.getPath());
            }

        }
        else if (requestCode == REQUEST_CAMERA) {

            Bitmap thumbnail = null;

            try {
                thumbnail = (Bitmap) data.getExtras().get("data");
            } catch (Exception e) {
                // TODO: handle exception
            }


            try {
                if(thumbnail != null){
                    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                    thumbnail.compress(Bitmap.CompressFormat.PNG, 100, bytes);

                    File file = new File(Environment.getExternalStorageDirectory()
                            + File.separator + Long.toString(System.currentTimeMillis())+".png");
                    file.createNewFile();
                    FileOutputStream fo = new FileOutputStream(file);
                    fo.write(bytes.toByteArray());
                    fo.close();
                    mStringGetImagePath = String.valueOf(file);
                    saveImage(mStringGetImagePath);
                    System.out.println("mStringGetImagePath "+mStringGetImagePath);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
}

openGalleryImage

private void openGalleryImage(Intent data) 
    {
        Uri selectedimg = data.getData();
        Uri uriselectedimage=data.getData();
        mString=uriselectedimage.getPath();
        try 
        {
            mInputStream=getActivity().getContentResolver().openInputStream(selectedimg);
        }
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }

        String[] path = { MediaStore.Images.Media.DATA };
        Cursor c = getActivity().getContentResolver().query(selectedimg, path, null, null,null);
        c.moveToFirst();
        int columnIndex = c.getColumnIndex(path[0]);
        selectedimage_paths=c.getString(columnIndex);

        uri_outputFileUri= Uri.parse(selectedimage_paths);
        c.close();
    }

When i run above code it gave me null pointer error at line this uri_outputFileUri= Uri.parse(selectedimage_paths);

this code is runable to all 4.0 ,4.1,4.2,4.3,4.4 device only problem in 5.0and 5.1 device

Aucun commentaire:

Enregistrer un commentaire