如何将从图库中检索到的图像保存到Android中的外部Sdcard内的新文件夹中

时间:2016-03-30 13:55:15

标签: android image android-sdcard android-external-storage

我在SDcard中创建了一个文件夹,并从图库到imageview检索图像。我想将此imageview文件图像保存到我的外部SD卡文件夹Multimedia / Images

private void showFileChooser() {
    Intent ginetnt = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(ginetnt,PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imgView.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public String getStringImage(Bitmap bmp){

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

    return encodedImage;
}

这是编辑创建文件夹SdCard

public void onStart(){
    super.onStart();

    String folder_main = "Multimedia";
    File Mul= new File (Environment.getExternalStorageDirectory(), folder_main);

    if(!Mul.exists()) {
        if(!Mul.mkdir()){
            Toast.makeText(this, Mul + " can't be created.", Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(this, Mul+" can be created.", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(this, Mul+" already exits.", Toast.LENGTH_SHORT).show();
    }

    File Images= new File (Environment.getExternalStorageDirectory() +"/" +folder_main, "Images");

    if(!Images.exists()) {
        if(!Images.mkdir()) {
            Images.mkdirs();
        }    
    }

    File Audio= new File (Environment.getExternalStorageDirectory() +"/" +folder_main, "Audio");

    if(!Audio.exists()) {
        if(!Audio.mkdir()) {
            Audio.mkdirs();
        }
    }

    File Video= new File (Environment.getExternalStorageDirectory() +"/" +folder_main, "Video");

    if(!Video.exists()) {
        if(!Video.mkdir()) {
            Video.mkdirs();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先从Bitmap中提取ImageView

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

然后您可以使用以下方法保存到您的图库。

public void saveBitmap(Bitmap bitmap) {
    String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/YourAlbum";
    File dir = new File(file_path);
    if (!dir.exists())
        dir.mkdirs();//create a file to write bitmap data

    File f = new File(dir, "yourImageName" + ".png");
    try {
        f.createNewFile();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
}