[英]Not able to delete image from external storage programmatically in android studio
我是 Android Studio 的初学者,我只想帮助从文件夹中删除图像,这里是我的代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
imageview = (ImageView)findViewById(R.id.imageView1);
bytearrayoutputstream = new ByteArrayOutputStream();
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Drawable drawable = getResources().getDrawable(R.drawable.barcode);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
file = new File( Environment.getExternalStorageDirectory() + "/SampleImage.png");
try
{
file.createNewFile();
fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(bytearrayoutputstream.toByteArray());
fileoutputstream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "Image Saved Successfully", Toast.LENGTH_LONG).show();
}
});
}
} 如果你支持我获取知识,那将非常有帮助
final String where = MediaStore.MediaColumns.DATA + "=?";
final String[] selectionArgs = new String[]{
f.getAbsolutePath()
};
final ContentResolver contentResolver = getContentResolver();
final Uri filesUri = MediaStore.Files.getContentUri("external");
contentResolver.delete(filesUri, where, selectionArgs);
// if (f.exists()) {
// contentResolver.delete(filesUri, where, selectionArgs);
// f.delete();
// }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.