将位图保存到相册

时间:2013-03-02 07:25:27

标签: android

当我使用以下代码时,位图将被保存并插入到库中“Camera”相册的末尾。

MediaStore.Images.Media.insertImage(getContentResolver(), bmp, null , null);

如何将位图保存到应用程序自己的相册?

1 个答案:

答案 0 :(得分:1)

private boolean createFolder(String folderName) {
    // TODO Auto-generated method stub
    boolean success = true;
    if(checkforMedia())
    {
        String newFolder = "/OnTheG/"+folderName;
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File myNewFolder = new File(extStorageDirectory + newFolder);
        success= myNewFolder.mkdirs();
    }

    else
    {
        success=false;
    }
    return success;


}

use this function to create your folder in sdcard.

Open the camera using this function to store your clicked image at your folder


public void takePhoto1() {
        if (!android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            Toast.makeText(Add_View_Images_Activity.this,
                    "Please insert SDcard for capturing photo.",
                    Toast.LENGTH_SHORT).show();
        } else {

            try {

                photo1=new File(path+"/"+System.currentTimeMillis()+".jpg");
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);              
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo1));
                cameraIntent.putExtra("return-data", true);

                startActivityForResult(cameraIntent, 4);
            } catch (Exception e) {
                Toast.makeText(Add_View_Images_Activity.this, ""+e, Toast.LENGTH_LONG).show();
            }
        }

    }


and If you pick a image from the gallery obtain it's path and then move it to your folder using this function


    private void moveTheImage(String path) {
            // TODO Auto-generated method stub
            //  File sd = Environment.getExternalStorageDirectory();
            //     File data = Environment.getDataDirectory();

            String sourceImagePath= path;
            System.out.println("Source path>>>>>>>>>"+path);

            String destinationImagePath= fWrapper.path+getTheName(path);

            File source= new File(sourceImagePath);
            File destination= new File(destinationImagePath);
            Log.d("before copying", "");
            if (source.exists()) {
                try
                {
                    FileChannel src = new FileInputStream(source).getChannel();
                    FileChannel dst = new FileOutputStream(destination).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }

                catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }