[英]Image not showing in gallery
我正在尝试将 imageview 中的图像保存到存储中。 下面是编码。 我能够成功地将图像保存到 memory 中,但它没有显示在图库中。 有人可以帮我吗? 我不知道我做错了什么。 我是 Android 开发和初学者阶段的新手。 谢谢你!
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private Button btn;
private ImageView imageView2;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
imageView = findViewById(R.id.image);
imageView2=findViewById(R.id.image2);
btn = findViewById(R.id.save);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bitmap=viewToBitmap(imageView,imageView.getWidth(),imageView.getHeight());
imageView2.setImageBitmap(bitmap);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + ".jpg";
String pathName = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/testApp";
File directory = new File(pathName);
if (!directory.exists()) {
directory.mkdirs();
}
File imageFile = new File(directory, imageFileName);
if (!imageFile.exists()) {
try {
imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
OutputStream fOut = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imageFile.getAbsolutePath());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
} catch (Exception e) {
e.printStackTrace();
Log.e("EXCEP ", e.getMessage());
}
// Show a Toast with the save location
Toast.makeText(getApplicationContext(), pathName, Toast.LENGTH_SHORT).show();
}
});
}
private static Bitmap viewToBitmap(View view, int widh, int hight)
{
Bitmap bitmap=Bitmap.createBitmap(widh,hight, Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(bitmap); view.draw(canvas);
return bitmap;
}
我猜您使用的是 Android X,尝试更改获取文件 URI 的方式
在 res/xml 中创建一个文件 provider_paths.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>
然后在你的 Manifest.xml 文件的 <application..> 中添加这个,
<provider
android:name="androidx.core.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>
然后是最后一部分,在您的代码中更改它
File f = new File(imageFile.getAbsolutePath());
Uri contentUri = Uri.fromFile(f);
至
File f = new File(imageFile.getAbsolutePath());
Uri contentUri = FileProvider.getUriForFile(getContext(),
getContext().getPackageName() + ".provider", f);
getExternalFilesDir()
中的文件不会被媒体存储扫描,因此不会被 Gallery spp 显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.