我只是想知道如何将我从图库中选择的图像保存到我自己的文件夹中以及重命名它,但所有属性都应该保持完好。好吧,我已经有了这个方法来自vogella的教程,你可以从画廊中选择并将其显示为预览。但不是预览我只是想做我上面提到的事情。这是代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/*this method is used for getting and selecting image from gallery and display it in the imageView*/
InputStream stream = null;
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
if (bitmap != null) {
bitmap.recycle();
}
stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
preview.setImageBitmap(bitmap);
//instead setting it in preview, save it in your image folder and rename it
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
我确实知道有关保存文件的一些部分,但我真的迷失了这个,所以希望有人可以帮助我。提前谢谢。
答案 0 :(得分:1)
private String saveToInternalSorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("directoryName", Context.MODE_PRIVATE);
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
// fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
答案 1 :(得分:1)
这样做
public String writeFileToInternalStorage(Context context, Bitmap outputImage) {
String fileName = Long.toString(System.currentTimeMillis()) + ".png";
final FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
outputImage.compress(CompressFormat.PNG, 90, fos);
}