我开发了一个允许以下内容的应用: 1.使用ImageView从库和视图加载图像。 2.将ImageView中的图像保存到图库中。 3.弹出警告对话框,单击“是”时将打开另一个布局。
我想将图像位图从第一个活动传递到第二个活动。我跟着几个例子,但图像没有通过。我的编码如下。
LoadImage.java:
public class LoadImage extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
Button buttonSave;
final Context context = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_image);
img = (ImageView)findViewById(R.id.ImageView01);
buttonSave = (Button) findViewById(R.id.button2);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
buttonSave.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
img.setDrawingCacheEnabled(true);
Bitmap bitmap = img.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/PARSE");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = "Photo-"+ n +".jpg";
File file = new File (newDir, fotoname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "Saved to your folder", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Review the Answer?");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to submit answer!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
Intent intent = new Intent(LoadImage.this, TestImage.class);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
intent.putExtra("byteArray", bs.toByteArray());
startActivity(intent);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
TestImage.java:
public class TestImage extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_image);
Intent intent = getIntent();
ImageView img = (ImageView) findViewById(R.id.ImageView01);
if(getIntent().hasExtra("byteArray")) {
ImageView previewThumbnail = new ImageView(this);
Bitmap bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
previewThumbnail.setImageBitmap(bitmap);
}
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("byteArray");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
img.setImageBitmap(bmp);
}
}
'意图'是第二个活动是灰色的,表明变量'intent'从未使用过。
有人可以帮我解决这个问题。任何帮助深表感谢。谢谢。
已更新!
LoadImage.java:
buttonSave.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
img.setDrawingCacheEnabled(true);
final Bitmap bitmap = img.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/PARSE");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = "Photo-"+ n +".jpg";
File file = new File (newDir, fotoname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "Saved to your folder", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Review the Answer?");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to submit answer!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
Intent intent = new Intent(LoadImage.this, TestImage.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("bitmapbytes",bytes);
startActivity(intent);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
TestImage.java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_image);
byte[] bytes = getIntent().getData().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView img = (ImageView) findViewById(R.id.ImageView01);
img.setImageBitmap(bmp);
}
}
答案 0 :(得分:1)
Bitmap实现了Parcelable,因此你总是可以在intent中传递它:
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
并在另一端检索它:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
答案 1 :(得分:1)
<强>活动强>
在Activites
之间传递位图Intent intent = new Intent(this, Activity.class);
intent.putExtra("bitmap", bitmap);
并在“活动课程”
中Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
<强>片段强>
在片段之间传递位图
SecondFragment fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);
在SecondFragment内部接收
Bitmap bitmap = getArguments().getParcelable("bitmap");
传输大位图(压缩位图)
如果您的活页夹事务失败,这意味着您将大型元素从一个活动转移到另一个活动,从而超出了活页夹事务缓冲区。
因此,在这种情况下,您必须将位图压缩为字节数组,然后在另一个活动中解压缩,就像这样
在FirstActivity中
Intent intent = new Intent(this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("bitmapbytes",bytes);
以及SecondActivity
byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
答案 2 :(得分:0)
是您可以通过Intent发送Bitmap
Intent intent = new Intent(Context,YourActivity.class); intent.putExtra(&#34;键&#34;,位图);
在YourActivity.class
中接收位图Intent intent = getIntent(); Bitmap image = intent.getParcelableExtra(&#34; key&#34;);
这适用于小尺寸位图
当Bitmap的大小很大时,它将无法通过Intent发送。 Beacuse Intent意味着在密钥值对中发送一小组数据。
为此您可以通过Intent
发送位图的Uri Intent intent =new Intent(Context ,YourActivity.class);
Uri uri;// Create the Uri From File Or From String.
intent.putExtra("key",uri);
在YourActivity.class中
Intent intent=getIntent();
Uri uri=intent.getParcelableExtra("key");
从Uri创建位图。
希望它会对你有所帮助。
答案 3 :(得分:0)
也许你的位图太大了。你会在你的logcat中找到FAILED BINDER TRANSACTION。所以,只需看看FAILED BINDER TRANSACTION while passing Bitmap from one activity to another