File copy from Gallery pick image to Internal Application Picture Directory Android

914 views Asked by At

Getting image Content from Gallery picture Now, I want to copy this image file(Which is picked from gallery) to my Internal App folder PICTURE directory(/storage/emulated/0/Android/data/MYApppackage/files/Pictures/).

I am creating new File in PICTURE Directory. file is created in internal AppDir / data / Pictures Directory and image with .JPG format but not showing image. Image is not visible. Image file is created with 0KB data.

I think its a file writing related issue or other i dont know.

 Intent intent ;
            if (Build.VERSION.SDK_INT < 19) {
                intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                MainActivity.state = "AddBalance";

                startActivityForResult(intent, REQUEST_GALLERY);
            } else {
                intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");

                startActivityForResult(intent, REQUEST_GALLERY);
            }

OnActivity Result of Gallery PickUp

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
 case REQUEST_GALLERY:
            if (data != null) {


if (data != null) {
                Bitmap bitmap=null;
                uri1 = data.getData();
       //         showPhotoDialog(uri1);
                try {
                //    bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri1);
                    InputStream image_stream = getActivity().getContentResolver().openInputStream(uri1);
                     bitmap= BitmapFactory.decodeStream(image_stream);


                } catch (IOException e) {
                    e.printStackTrace();
                }
                Log.e(TAG,"Data URI----"+data.getData());
                    try {
                        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                        String imageFileName = "PNG_" + timeStamp + "_";
                        File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                        File image = File.createTempFile(
                                imageFileName, //  prefix
                                ".PNG",        //  suffix
                                storageDir    //   directory
                        );

                     //   File imageFile= new File(storageDir,imageFileName);
                        FileOutputStream out;
                        try {
                           // out = getActivity().openFileOutput(image.getName(), Context.MODE_PRIVATE);
                            out = new FileOutputStream(image.getName());
                          /*  ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                            byte[] byteArray = stream.toByteArray();
                           out.write(byteArray);*/
                           bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                            out.close();
                        } catch (Exception e) {e.printStackTrace();}
//         copyFile(new File(getRealPathFromURI(uri1)), image);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();

            }

            }`
     }

Edit : Infos

getting value of uri1 from pickup gallery image : content://com.android.providers.media.documents/document/image%3A25877

I am Trying to write bitmap to File. Here is my Code. getBitmap from REQUEST_GALLERY and URI works fine . I am making File in Enviroment.PICTURE directory and write via OutPutStream. but Still File : PNG_54342323111.PNG

I also tried with ByteArrayOutputStream which you can see in comments but didnt works. also tried with OpenFileOutput but not working.

0

There are 0 answers