如何将Android中的文件保存到特定文件夹?

时间:2016-08-30 16:21:07

标签: android

 btnsave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivdisplayphoto.setDrawingCacheEnabled(true);
              Bitmap bitmap = ivdisplayphoto.getDrawingCache();
              String message = getIntent().getExtras().getString("");//== String message= "/folder1/folder2/"

                String root = (Environment.getExternalStorageDirectory().getPath()+message);
                
                //  String root = (Environment.getExternalStorageDirectory().getPath()+""/folder1/folder2/");
                text.setText(root);
             final   File newDir = new File(root + "//saved_imag");
                newDir.mkdirs();
                Random gen = new Random();
                int n = 10000;
                n = gen.nextInt(n);
                String fotoname = "photo-"+ n +".jpg";
                File file = new File (newDir, fotoname);
                if (file.exists ()){
                    file.delete();
                }
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    out.close();
                    Toast.makeText(getApplicationContext(), "safed to your folder", Toast.LENGTH_SHORT ).show();

                } catch (Exception e) {

                }


            }
        });

嘿,我是Android新手,我一直在尝试制作应用。 部分将相机图像保存到新创建的文件夹中时遇到问题。 问题在于消息变量我可以创建文件“newDir”如果我使用简单的字符串“/ folder1 / folder2 /”,但如果我使用“消息”变量 我无法创建它

3 个答案:

答案 0 :(得分:0)

  //  Your problem with message variable. Below is working code please go through it.

                    btnsave.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(cameraIntent, 0);
                        }
                    });

        //save you image when calling the onActivityResult method after capture image.

        //When user click image vai camera then directly store to save_image folder.
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (resultCode == RESULT_OK) {
                    Bundle extras = data.getExtras();
                    Bitmap mImageBitmap = (Bitmap) extras.get("data");
                    String root = Environment.getExternalStorageDirectory().toString();
                    File myDir = new File(root + "/saved_images");
                    myDir.mkdirs();
                    Random generator = new Random();
                    int n = 10000;
                    n = generator.nextInt(n);
                    String fname = "Image_" + n + ".jpg";
                    File file = new File(myDir, fname);
                    if (file.exists())
                        file.delete();
                    try {
                        FileOutputStream out = new FileOutputStream(file);
                        mImageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                        out.flush();
                        out.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

答案 1 :(得分:0)

  // But the problem it is I want to record the photo in the path rot=root+message 
// In another activity I have a qr-code scanner, every qr-code scanned corresponds to a path the content of every qr-code pass of the first activity to the second, the content is stored in the variable "message".
//Then i am obliged to use the variable "message" so that I draw to change the path  to store my photo.


String message = getIntent().getExtras().getString("");
                 String     root=Environment.getExternalStorageDirectory().toString();
                String rot = root+message;
                //  String root = (Environment.getExternalStorageDirectory().getPath()+""/folder1/folder2/");

             final   File newDir = new File(rot + "/dossier_photos");

答案 2 :(得分:0)

在这里你用Path做了一个简单的错误:

Environment.getExternalStorageDirectory().getPath() = /storage/emulated/0
Environment.getExternalStorageDirectory().getPath()+message = /storage/emulated/0message

所以你得到了错误, 这里的路径应该如下:

 String root = (Environment.getExternalStorageDirectory().getPath()+"/"+message);
相关问题