【问题标题】:save the image to the gallery in android将图像保存到android中的画廊
【发布时间】:2017-08-18 16:25:53
【问题描述】:

如何将相机拍摄的图像保存到图库中?我有这个代码,但不保存图像

private File createImageFile() throws IOException {
// Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName, /* prefix */
                ".jpg", /* suffix */
                storageDir /* directory */
        );

// Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

几乎都用这个

private void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

但没有积极的结果,在我的清单中我使用了这个权限

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

【问题讨论】:

  • 你添加运行时权限了吗
  • 出现的唯一权限是如果我想授予使用相机的权限
  • 您在哪个操作系统版本中运行您的应用程序
  • 在 6.0 和 7.1 中
  • 那么您需要询问运行时权限才能将图像存储在内部存储中我的朋友read from docs about runtime permsiion

标签: android


【解决方案1】:

从android版本M及以上,任何危险权限都需要运行时权限-https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous

public void checkPermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (!(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (!(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
        Log.d(TAG, "Storage Permission not granted");   
    } else {
        // Permission granted - resume
    }
}

【讨论】:

猜你喜欢
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多