1
我使用下面的代碼保存/載入我的圖片圖片保存在仿真器,但不是在設備
OnCreate(){
super.onCreate();
setContentView(...)
....
setImage();
}
protected void onResume(){
super.onResume();
setImage();
}
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
setImage();
}
protected void onPostResume(){
super.onPostResume();
setImage();
}
private void setImage(){
if(loadPicture(getIntent().getStringExtra("position"),bitmap) != null){
Toast.makeText(this, "not null", Toast.LENGTH_SHORT).show();
imageView.setImageBitmap(loadPicture(getIntent().getStringExtra("position"), bitmap));
}
}
private void takePicture() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(),
"Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, 0);
}
private void savePicture(String filename, Bitmap b, Context ctx){
try {
ObjectOutputStream bos;
FileOutputStream out;// = new FileOutputStream(filename);
out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
bos = new ObjectOutputStream(out);
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
if(b.compress(Bitmap.CompressFormat.PNG, 100, bos) == true)
Toast.makeText(this, "returned true", Toast.LENGTH_LONG).show();
bos.flush();
// bos.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private Bitmap loadPicture(String filename, Bitmap b){
//Drawable myImage = null;
try {
FileInputStream fis = openFileInput(filename);
ObjectInputStream bis = null;
try {
bis = new ObjectInputStream(fis);
} catch (StreamCorruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// myImage = Drawable.createFromStream(ois, filename);
b = BitmapFactory.decodeStream(bis);
try {
//bis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//return myImage;
return b;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, bitmap.getHeight()/2, bitmap.getWidth()/2, false));
//previos imageView.setImageBitmap(bitmap)
savePicture(getIntent().getStringExtra("position"), bitmap,
getApplicationContext());
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
這完全適用將圖像保存在模擬器(即樣本圖像)中,但它在真實手機中不起作用。我把圖像,然後按手機上的「OK」圖標後,有大約20秒空白屏幕。然後它崩潰。
你可以粘貼logcat的異常堆棧? –
如何獲取logcat的堆棧,因爲它工作正常,在模擬器上...在設備上,我懷疑這是一個超時,因爲我覺得像後的應用程序崩潰延長了一段時間......可能會導致這種情況?我認爲這與我的方式有關保存文件 – Kgrover
您可以使用USB電纜將IDE連接到設備,並在設備上啓用調試。請參閱http://developer.android.com/guide/developing/device.html](http://developer.android.com/guide/developing/device.html) –