【问题标题】:Can't see pictures saved in a specific folder看不到保存在特定文件夹中的图片
【发布时间】:2013-08-12 09:07:14
【问题描述】:

我的应用程序使用手机摄像头拍照并将它们保存在特定文件夹中。我无法通过 Android Gallery 或插入我的电脑看到它们,但我可以使用文件管理器应用程序。 我找到了解决方案:我使用文件管理器应用重命名图片,然后我可以在图库中看到它们。

我使用的代码是:

Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String dirName = Environment.getExternalStorageDirectory()+"/MyAPP/APP"+ n +".jpg";
Uri uriSavedImage = Uri.fromFile(new File(dirName));
camera.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage);
startActivityForResult(camera, 1);
n++;

AndroidManifest:

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

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />

【问题讨论】:

  • 您可以打印 dirName 变量并将其与您使用文件管理器查看的路径进行比较吗?
  • 是的,是一样的。它拯救了他们,但发生了一些奇怪的事情
  • 尝试“/mnt/sdcard”而不是Environment.getExternalStorageDirectory(),

标签: android android-4.0-ice-cream-sandwich android-gallery android-camera-intent


【解决方案1】:

用于将图像保存到特定文件夹,

private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) {

File direct = new File(Environment.getExternalStorageDirectory() + "/DirName");

if (!direct.exists()) {
    File wallpaperDirectory = new File("/sdcard/DirName/");
    wallpaperDirectory.mkdirs();
  }

    File file = new File(new File("/sdcard/DirName/"), fileName);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();

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

}

试试这个以访问该图像

private void showImage(String imageName) {

    File direct = new File(Environment.getExternalStorageDirectory() + "/SlamImages");

    if (direct.exists()) {

        File f = new File(Environment.getExternalStorageDirectory() + "/SlamImages/" + imageName);

        if (f.exists() && !imageName.equals("")) {
            Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
            imgUserLogo.setImageBitmap(bmp);
        } else {
            imgUserLogo.setImageResource(R.drawable.friend_image);
            Toast.makeText(FriendsDetailsActivity.this, "Image Does not exist", Toast.LENGTH_SHORT).show();
        }

    }

}

【讨论】:

  • 我也有同样的问题
  • 请给logcat
【解决方案2】:

我只需要添加一些代码来扫描图片并将其添加到图库中

private void addGallery() {
    Intent mediaScanIntent = new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    String currentPath = Environment.getExternalStorageDirectory()
            + "/MyAPP/APP" + n + ".jpg";
    File f = new File(currentPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

我在 onActivityResult 中添加了它,它可以工作

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 2012-01-23
    • 2013-05-27
    • 2015-06-24
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多