Android 将位图保存到SD卡,不创建任何文件
我想在按下按钮时将位图作为jpg文件保存到SD卡。当我运行以下代码时,我没有收到任何错误,但当我检查手机上的SD卡时,没有创建任何文件。我做错了什么Android 将位图保存到SD卡,不创建任何文件,android,android-sdcard,android-bitmap,Android,Android Sdcard,Android Bitmap,我想在按下按钮时将位图作为jpg文件保存到SD卡。当我运行以下代码时,我没有收到任何错误,但当我检查手机上的SD卡时,没有创建任何文件。我做错了什么 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int permission = ActivityCompat.checkSelfPermiss
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
String root = Environment.getExternalStorageDirectory().toString();
//String root = System.getenv("SECONDARY_STORAGE");
File myDir = new File(root + "/Screenshots");
myDir.mkdirs();
Random generator = new Random();
int n = generator.nextInt(10000);
String fname = n+".jpg";
File file = new File(myDir, fname);
Log.d("DD",""+file);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
rBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
我拥有清单中的权限,但仍然没有生成任何文件:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我做过一些研究,认为getExternalStorageDirectory()不提供SDcard的路径,但以下替代方法提供空值:
字符串root=System.getenv(“辅助存储”) 找到了@commonware指出的答案,即调用MediaScannerConnection.scanFile,这样MediaStore就可以发现该文件并在文件资源管理器中列出它们。@greenapps还建议,文件位于外部存储器0中,而不是SD卡中
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
String root = Environment.getExternalStoragePublicDirectory("").toString();
//String root = System.getenv("SECONDARY_STORAGE");
File myDir = new File(root + "/Screenshots");
if (!myDir.exists())
if (!myDir.mkdirs())
{Toast.makeText(activity, "make dir failed.", Toast.LENGTH_SHORT).show(); return;}
Random generator = new Random();
int n = generator.nextInt(10000);
String fname = n+".jpg";
File file = new File(myDir, fname);
Log.d("DD",""+file);
try {
FileOutputStream out = new FileOutputStream(file);
rBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
//add mediastore discovery
MediaScannerConnection.scanFile(activity, new String[] {file.getAbsolutePath()},new String[] {null}, null);
}
}
删除文件并保存位图的原因。如果(!file.exists()){file.createNewFile();},请使用
如果存在相同的文件名,则只删除现有文件。如果要编辑该文件,则应存在该文件。如果删除现有文件,则该路径中没有文件,因此无法对其进行编辑。尝试使用上面的代码只是尝试了一下,但仍然没有看到任何文件:Try{file.createNewFile();FileOutputStream out=newfileOutputStream(文件);rBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);out.flush();out.close();}catch(异常e){e.printStackTrace();}