0

我的应用程序使用手机摄像头拍照并将它们保存在特定文件夹中。我无法通过 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++;

安卓清单:

<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" />
4

2 回答 2

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

    }

}
于 2013-08-12T09:28:57.770 回答
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 并且它有效

于 2013-08-14T07:36:20.090 回答