【问题标题】:Can I save photo of android app on sd card? [closed]我可以将 Android 应用程序的照片保存在 SD 卡上吗? [关闭]
【发布时间】:2018-12-24 09:50:54
【问题描述】:

我正在构建一个应用程序来从相机拍照,但我不知道我可以将我的应用程序照片保存在 avd android 的 sd 卡上(我的 AVD:Nexus 5X API 26 Android 8.0)。 我拍了照片并在我的应用程序上展示了我的照片,但我在为我的应用程序创建的文件夹中找不到它(在 SDCard 中)。谢谢。

【问题讨论】:

标签: android android-camera android-sdcard android-camera-intent


【解决方案1】:

首先在您的清单中添加权限:

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

所以你需要获取你的 Bitmap。你可以尝试从 ImageView 中获取它,例如:

BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable();
Bitmap bitmap = drawable.getBitmap();

你必须从 SD 卡进入目录

    File sdCardDirectory = Environment.getExternalStorageDirectory();
// Next, create your specific file for image storage:

    File image = new File(sdCardDirectory, "test.png");
//After that, you just have to write the Bitmap :

    boolean success = false;

    // Encode the file as a PNG image.
    FileOutputStream outStream;
    try {

        outStream = new FileOutputStream(image);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
        /* 100 to keep full quality of the image */

        outStream.flush();
        outStream.close();
        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (success) {
        Toast.makeText(getApplicationContext(), "Image saved with success",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
                "Error during image saving", Toast.LENGTH_LONG).show();
    }

【讨论】:

  • > android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • @eyllanesc 嗨,我想添加权限,但发送时未显示!谢谢!
  • 感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多