无法查看保存在特定文件夹中的图片

时间:2013-08-12 09:07:14

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

我的应用使用手机相机拍照并将其保存在特定文件夹中。我无法通过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" />

2 个答案:

答案 0 :(得分:0)

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

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();
        }

    }

}

答案 1 :(得分:0)

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

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中添加了它并且它可以正常工作