请帮助我如何将图像保存到设备,然后在下一个活动中获取图像。有人告诉我,如果我想使用高质量的图像,不要在意向中传递。我必须把图像保存到设备上,这样它就不需要在意图中传递,但我不确定是如何传递的
这是我的onPostExecute代码
@Override
protected void onPostExecute(Face[] faces) {
pd.dismiss();
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
Gson gson = new Gson();
String data = gson.toJson(faces);
if (faces == null || faces.length == 0) {
makeToast("No faces detected. You may not have added the API Key or try retaking the picture.");
} else {
intent.putExtra("list_faces", data);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("image", byteArray);
startActivity(intent);
}
}
};
detectTask.execute(inputStream);
}
在第一个活动中将位图保存在内部内存上
fun saveFile(bitmap: Bitmap, context: Context):String?{
var fileName:String?= "myImage"
try {
val fo = context.openFileOutput(fileName, Context.MODE_PRIVATE)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fo)
fo.close()
} catch (e:Exception) {
e.printStackTrace()
fileName = null
}
return fileName;
}
将fileName放入extra中,并将其发送给第二个活动:
val filename=saveFile(bitmap,requireContext())
val intent = Intent(getApplicationContext(), ResultActivity.class)
intent.putExtra("fileName", filename)
startActivity(intent)
在第二个活动中,您可以获得位图:
val fileName = intent.getStringExtra("fileName")
val bitmap = BitmapFactory.decodeStream(context.openFileInput(fileName))