Android 将片段保存到图像文件中,并将其保存到手机的内部硬盘驱动器中
我想制作一个png文件片段,并将其保存在设备上。但是我不知道如何将文件保存到设备内部存储器。我在尝试该程序时遇到异常“FileNotFoundException e”Android 将片段保存到图像文件中,并将其保存到手机的内部硬盘驱动器中,android,android-bitmap,fileoutputstream,Android,Android Bitmap,Fileoutputstream,我想制作一个png文件片段,并将其保存在设备上。但是我不知道如何将文件保存到设备内部存储器。我在尝试该程序时遇到异常“FileNotFoundException e” Button save = (Button) findViewById(R.id.save); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(Vie
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View fragment = (View) findViewById(R.id.fragment2);
viewToBitmap(fragment);
}
}
);
}
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
try {
FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.close();
} catch (FileNotFoundException e) {
Toast.makeText(this, "Error1", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "Error2", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return bitmap;
}
您正在创建FileOutputStream,但在此之前尚未创建文件。在将输出流写入文件之前,需要创建文件文件夹和文件。为您的方法更正了下面的代码
viewToBitmap
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
try {
File tempFolder = new File(Environment.getExternalStorageDirectory() + "/path/to");
if (!tempFolder.exists()) tempFolder.mkdirs();
File tempFile = File.createTempFile("tempFile", ".jpg", tempFolder);
FileOutputStream output = new FileOutputStream(tempFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.close();
} catch (FileNotFoundException e) {
Toast.makeText(this, "Error1", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "Error2", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return bitmap;
}
您正在创建FileOutputStream,但在此之前尚未创建文件。在将输出流写入文件之前,需要创建文件文件夹和文件。为您的方法更正了下面的代码
viewToBitmap
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
try {
File tempFolder = new File(Environment.getExternalStorageDirectory() + "/path/to");
if (!tempFolder.exists()) tempFolder.mkdirs();
File tempFile = File.createTempFile("tempFile", ".jpg", tempFolder);
FileOutputStream output = new FileOutputStream(tempFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.close();
} catch (FileNotFoundException e) {
Toast.makeText(this, "Error1", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "Error2", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return bitmap;
}