【发布时间】:2020-01-11 09:45:24
【问题描述】:
我是 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();
}
});
}
} 如果您支持我获取知识,那将非常有帮助
【问题讨论】:
-
File.delete() ?
-
Toast.makeText(MainActivity.this, "Image Saved Successfully", Toast.LENGTH_LONG).show();不。你不能在这里说,因为也许有一个例外。将 toast 放入 try 块中,将不同的 Toast 放入 catch 块中,以在失败时通知用户和您自己。 -
file.createNewFile();删除该行。该文件将由新的 FileOutputStream() 创建。 -
当问题与 Android Studio 无关时,不明白为什么人们一直在他们的问题标题后面附加“in Android Studio”......