如何在android设备的内部存储器中保存图像?
我想在设备的内部存储器中保存映像,因为许多设备没有外部存储器或SD卡如何在android设备的内部存储器中保存图像?,android,save,Android,Save,我想在设备的内部存储器中保存映像,因为许多设备没有外部存储器或SD卡 InputStream y11 = getResources().openRawResource(to); Bitmap b11 = BitmapFactory.decodeStream(y11); File direct = new File(Environment.getExternalStorageDirectory() .toString() + "/newimages"); direct.mkdir
InputStream y11 = getResources().openRawResource(to);
Bitmap b11 = BitmapFactory.decodeStream(y11);
File direct = new File(Environment.getExternalStorageDirectory()
.toString() + "/newimages");
direct.mkdirs();
String fName = "Image-" + String.valueOf(System.currentTimeMillis())+ ".jpg";
File file = new File(direct, fName);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
b11.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
// out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+Environment.getExternalStorageDirectory())));
} catch (Exception e) {
e.printStackTrace();
}
}
您可以使用此代码检查SD卡是否存在:
Boolean haveSd= android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(haveSd)
{
// Work on device-sd
}
else
{
//Work on device
}
如果要保存在内部存储器中,请执行以下操作:
public boolean saveImageToInternalStorage(Bitmap image) {
try {
// Use the compress method on the Bitmap object to write image to
// the OutputStream
FileOutputStream fos = context.openFileOutput("desiredFilename.png", Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
image.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
return true;
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
return false;
}
}
StackOverflow用于编程问题。你的问题是什么?这只是我的编程问题。此代码帮助我仅在SD卡中保存图像。但若设备并没有SD卡呢?此代码不适用于此。那么解决方案是什么呢?“此代码仅帮助我将图像保存在SD卡中”--不,它将图像保存到外部存储器。外部存储不是“SD卡”。“但若设备并没有SD卡呢?”——几乎所有Android设备都有外部存储。也就是说,您可以使用
getFilesDir()
将映像保存在内部存储器上。