0

从图库图片中获取图片内容 现在,我想将这个image file(从图库中选取)复制到我的内部应用程序文件夹 PICTURE 目录(/storage/emulated/0/Android/data/MYApppackage/files/Pictures/)。

我正在创建new File. PICTURE Directory文件是在内部AppDir / data / Pictures Directory和图像中创建的,.JPG格式但不显示图像。图像不可见。图像文件是用0KB数据创建的。

我认为这是与文件写入相关的问题或其他我不知道的问题。

 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);
            }

Gallery PickUp的OnActivity结果

 @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();

            }

            }`
     }

编辑:信息

从拾取图库图像中获取 uri1 的值:content://com.android.providers.media.documents/document/image%3A25877

我正在尝试将位图写入文件。这是我的代码。getBitmapREQUEST_GALLERY并且URI 工作正常。我正在Enviroment.PICTURE目录中制作文件并通过OutPutStream. 但仍然文件: PNG_54342323111.PNG

我也试过ByteArrayOutputStream你可以在评论中看到但没有用。也试过OpenFileOutput但不工作。

4

0 回答 0