简体   繁体   English

Android将图像保存在外部存储中

[英]Android save image in external storage

I am working on Android for saving multiple image in sd card but its getting save in gallery also and specific folder also... any one can help on this thanks.. 我正在使用Android在SD卡中保存多个图像,但是它也保存在图库中并且也保存在特定的文件夹中。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        // 2
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

        mImage.setImageBitmap(thumbnail);
        CharSequence time2 = android.text.format.DateFormat.format(
                "yymmddhhmmss", new java.util.Date());
        String date1 = time2.toString();

        //ByteArrayOutputStream bytes = new ByteArrayOutputStream();
         try {

                thumbnail .compress(CompressFormat.JPEG, 100, new FileOutputStream(Environment.getExternalStorageDirectory()
        .getAbsolutePath()  + "/mypics/img" + date1 + ".png"));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    } else
        Toast.makeText(getBaseContext(), "Please take snap again",
                Toast.LENGTH_LONG).show();
}       

try like this:: 尝试这样:

Use this function to save your bitmap in SD card 使用此功能将位图保存在SD卡中

private void SaveIamge(Bitmap finalBitmap) {

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);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

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

and add this permision in manifest: 并在清单中添加以下内容:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

to save in gallery just add this one: 保存在图库中,只需添加以下内容:

sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
        Uri.parse("file://" + Environment.getExternalStorageDirectory())));

If you do not want images inside the /mypics/img/ folder to be shown in the gallery. 如果您不希望/mypics/img/文件夹中的图像显示在图库中。 All you need to do is: 您需要做的只是:

Include an empty file named .nomedia in your /mypcis/img/ directory (note the dot prefix in the filename). /mypcis/img/目录中包含一个名为.nomedia的空文件(请注意文件名中的点前缀)。 This will prevent Android's media scanner from reading your media files and including them in apps like Gallery or Music. 这将阻止Android的媒体扫描器读取您的媒体文件,并将它们包括在诸如Gallery或Music之类的应用程序中。

Note: For some devices, this only works for new folders (not those which are already scanned) So my recommendation would be to create a new folder with the .nomedia file and store your images to that. 注意:对于某些设备,这仅适用于新文件夹(不适用于已扫描的文件夹),因此,我的建议是使用.nomedia文件创建一个新文件夹并将图像存储到该文件夹​​。 Now they will not appear in the gallery. 现在它们将不会出现在图库中。 Only in the external SD card folder. 仅在外部SD卡文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM