[将图像从Android应用保存到内部存储中

问题描述 投票:1回答:1

我希望在内部存储器中创建一个名为“ Demo”的文件夹,并将其图像写入其中。我尝试了以下代码,但没有帮助。该代码编译没有任何错误,但是没有给出期望的结果。

    File filePath = Environment.getExternalStorageDirectory();
    File dir = new File(filePath.getAbsolutePath()+"/Demo/");
    dir.mkdir();
    file = new File(dir,System.currentTimeMillis()+".jpg");
    try {
        outputStream = outputStream = new FileOutputStream(file);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    Toast.makeText(this, "Image saved to internal storage", Toast.LENGTH_SHORT).show();
    try {
    outputStream.flush();
    outputStream.close();}
    catch (Exception e)
    {
        e.printStackTrace();
    }

我确实收到一句吐司说“图像已保存到内部存储器”,但没有得到保存。

java android file android-studio fileoutputstream
1个回答
0
投票

您可以在授予用户权限并显示清单后使用此代码:

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

这里是保存照片的代码:

private void SaveThePhoto()
{

    FileOutputStream outputStream = null;

    File filePath = Environment.getExternalStorageDirectory();
    File dir = new File(filePath.getAbsolutePath()+ "/Demo/");
    dir.mkdir();
    String fileName = String.format("Demo" + System.currentTimeMillis() + ".jpg");


    File outFile = new File(dir, fileName);
    Toast.makeText(Activity.this, getString(R.string.Toast_SavePhoto) + filePath.getAbsoluteFile() + "/Demo/" + fileName, Toast.LENGTH_LONG).show();

    try {
        outputStream = new FileOutputStream(outFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(outFile));
        sendBroadcast(intent);

    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

0
投票

您绝对需要检查日志猫,以确保一切正常!但我也建议您编写如下代码:

   File filePath = Environment.getExternalStorageDirectory();
    File dir = new File(filePath.getAbsolutePath()+"/Demo/");
    dir.mkdir();
    file = new File(dir,System.currentTimeMillis()+".jpg");
    try {
        outputStream  = new FileOutputStream(file);
        outputStream.flush();
        outputStream.close();
        Toast.makeText(this, "Image saved to internal storage", Toast.LENGTH_SHORT).show();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

,然后检查您是否仍然可以得到吐司。

这里有一段对我有用的代码:

/* Checks if external storage is available for read and write */
    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }

    protected static void saveImage(Context context){
        boolean sucess = true, deleted = true, created;
        if (!folderImages.exists()) {
            sucess = folderImages.mkdir();
            // initiate media scan and put the new things into the path array to
            // make the scanner aware of the location and the files you want to see
            MediaScannerConnection.scanFile(context, new String[] {folderImages.toString()}, null, null);
        }

        try {
              // create image file
              File file = new File(PATH+File.separator+PATH_IMAGE, IMAGE_NAME);
              if (file.exists()) {
                // delete image that already exist with same name
                deleted = file.delete();
              }
              // create the new image
              created = file.createNewFile();
              if (deleted && created && isExternalStorageWritable) {
                 // save image
                 OutputStream stream = new FileOutputStream(file);
                 bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream); // Compressing is often interesting
                 stream.flush();
                 stream.close();
             }
        } catch (IOException e) {
             e.printStackTrace();
        }
    }

在尝试编写之前,请务必记住先在应用中寻求许可,并在编写之前始终检查您是否具有许可。

我从您的代码中看到的区别是我使用的MediaScannerConnectionFile.separador,如果您在查看日志猫后看到任何错误,则没有发现,我建议尝试添加类似的东西。

OBS:这一段代码很旧,我认为它不是最好的代码,因此您可以在将图像保存到内部存储here中获得更多信息。但首先进入您的日志目录:)!

© www.soinside.com 2019 - 2024. All rights reserved.