android - 将位图保存到相册

标签 android

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

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

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

最佳答案

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

关于android - 将位图保存到相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15171962/

相关文章:

php - 在 google App Engine 中为移动应用程序托管带有 MySQL 的 PHP API。如何?是好是坏?

android - APK 大小、Twilio、拆分和架构兼容性

android - 如何直接在android中比较两个图像背景

android - Firebase Crashlytics 不支持 NDK?

Android:用可点击的 HTML 链接替换文本 URL

android - Google PlaceDetectionClient 与 FusedLocationProviderClient

android - Firebase 控制台 : How to specify click_action for notifications

android - 如何将参数传递给 Android 中的对话框?

android - 是否可以知道 Android 应用程序的来源(例如 Google Play)?

Android NDK ARM build设置以在大多数设备上运行?