我正在尝试截取视图并使用图像查看器打开,Android 7.0及以下版本可以顺利运行但是7以上版本无效。 我正在创建一个目录并将屏幕截图保存到该文件夹中,屏幕截图保存到该文件夹但是它没有在y库中查看,我可以使用文件查看器找到该文件。 我的代码
MainActivity.java
private static final int PERMISSION_REQUEST_CODE = 1;
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
File dir = new File(Environment.getExternalStorageDirectory(), "file status");
try {
if (dir.mkdir()) {
Toast.makeText(ApplicationStatus.this, "in.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ApplicationStatus.this, "ot.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "file status" + "/" + now + ".jpg";
View v1 = relativeLayout;
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
try {
Toast.makeText(ApplicationStatus.this, "dir:." + imageFile, Toast.LENGTH_LONG).show();
File file = new File(String.valueOf(imageFile));
if (file.exists()) {
Toast.makeText(ApplicationStatus.this, "dir:.true", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ApplicationStatus.this, "dir:.false", Toast.LENGTH_LONG).show();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(ApplicationStatus.this, BuildConfig.APPLICATION_ID + ".provider", imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
} catch (Exception e) {
Log.e("MYAPP", "exception", e);
}
}
private void requestAppPermissions() {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return;
}
if (hasReadPermissions() && hasWritePermissions()) {
return;
}
ActivityCompat.requestPermissions(this,
new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, PERMISSION_REQUEST_CODE); // your request code
}
private boolean hasReadPermissions() {
return (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
}
private boolean hasWritePermissions() {
return (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
}
}
RES / XML / provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
清单
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
答案 0 :(得分:1)
将图像保存到内存后,解决方案正在使用MediaScannerConnection
。
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});