0

我想将图像文件从一个位置复制到另一个位置,我的图像文件存储在 data

android模拟器中的文件夹,我想将其复制或移动到数据文件夹的子文件夹中。

为此我使用过

文件、文件输入流和文件输出流。现在我无法理解如何给出路径

文件存储在android模拟器中。请帮我解决这个问题。

4

1 回答 1

0

试试这个仅用于图像..

    private File imageFile;

        private boolean writeFile() {
            imageFile = new File(
                    getApplicationContext().getFilesDir() + "/images/",
                    "sample.png");

           /* 
               Image save to  "/data/data/com.mycomp.android.test/files/images/sample.png"    
               com.mycomp.android.test  is your package name        
           */

            String savePath = imageFile.getAbsolutePath();
            System.out.println("savePath :" + savePath + ":");

            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(savePath, false);
            } catch (FileNotFoundException ex) {
                String parentName = new File(savePath).getParent();
                if (parentName != null) {
                    File parentDir = new File(parentName);
                    if ((!(parentDir.exists())) && (parentDir.mkdirs()))
                        try {
                            fileOutputStream = new FileOutputStream(savePath, false);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }

                }
            }


          /* 
              path which image you need to move on data folder
           */


            String path = Environment.getExternalStorageDirectory()
                    + "/amit/amit.png";
            File imgFile = new File(path);

            Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

            byte[] imgBuffer = stream.toByteArray();

            boolean result = true;

            try {
                fileOutputStream.write(imgBuffer);
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                result = false;
            } catch (IOException e2) {
                e2.printStackTrace();
                result = false;
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        result = false;
                    }
                }
            }

            return result;

        }
于 2013-10-17T11:38:13.027 回答