java - 无法在 android studio 中以编程方式从外部存储中删除图像

标签 java android file

我是 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();
// }

关于java - 无法在 android studio 中以编程方式从外部存储中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59693283/

相关文章:

java - 删除从输入文件到 spring 批处理程序的所有行中的尾随空格

java - 按下时按键监听器不工作

java - 如何将属性文件中的空值添加到 map

c - 在 C 文件中读取(并转换)矩阵

透明填充外圆内带有实心填充圆的android Layerlist

android - 删除绘图缓存

c - 使用 Execl 执行我的程序

java - 将现有私钥导入 BKS Keystore

java - 来源和资源之间的区别

android - Android Google Map 中的高效 map 叠加层