Shw*_*eta 6 android bitmap bitmapimage android-intent
在我的应用程序中,我正在显示来自图库的图像,一旦我选择一个图像,图像应该被发送到新活动,其中所选图像将被设置为背景.但是,我能够获取图像从图库,但一旦我选择一个应用程序崩溃.谢谢提前
活动-1(图像显示在图库中,所选图像将发送到新活动)
public class Gallery extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);
Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);
// Bitmap croppedImage = BitmapFactory.decodeFile(croppedImage);
Intent intent = new Intent(Gallery.this,GesturesActivity.class);
intent.putExtra("bmp",croppedImage);
startActivity(intent);
Log.v("sending image","sending image");
}
}
}
Run Code Online (Sandbox Code Playgroud)
活性-1(XML)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>
<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_weight="1" android:layout_height="wrap_content"></ImageView>
<Button
android:layout_height="wrap_content"
android:text="Load Picture"
android:layout_width="wrap_content"
android:id="@+id/buttonLoadPicture"
android:layout_weight="0"
android:layout_gravity="center"></Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
活动-2(应将所选图像设置为屏幕背景图像的活动)
public class GesturesActivity extends Activity {
private final int MENU_CAMERA = Menu.FIRST;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Bitmap bmp = (Bitmap)this.getIntent().getParcelableExtra("bmp");
BitmapDrawable background = new BitmapDrawable(bmp);
getWindow().setBackgroundDrawable(background); //background image of the screen
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.advert);
View view = new SandboxView(this, bitmap);
setContentView(view);
}
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
menu.add(0, 11, 0, "Take Snapshot");
return super.onPrepareOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
Dip*_*iya 18
有3种解决方案可以解决这个问题.
1)首先将图像转换为字节数组然后传入Intent并在下一个活动中从Bundle获取字节数组并转换为图像(位图)并设置为ImageView.
将位图转换为字节数组: -
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Run Code Online (Sandbox Code Playgroud)
将字节数组传递给intent: -
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
从Bundle获取字节数组并转换为位图图像: -
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
Run Code Online (Sandbox Code Playgroud)
2)首先将图像保存到SD卡中,然后在下一个活动中将此图像设置为ImageView.
3)将Bitmap传递给Intent并从bundle中获取下一个活动中的位图,但问题是如果你的位图/图像大小很大,那时图像不会在下一个活动中加载.
小智 6
由于您是从Gallery中检索图像,为什么不将id传递给下一个活动并检索该活动中的图像而不是传递图像?这将有助于您记忆和表现.
Bitmap实现了Parcelable,因此您可以始终在intent中传递它.尝试以下代码:
第一项活动.
Intent mIntent = new Intent(this, ActivityTwo.class);
mIntent.putExtra("bmp_img", bmp);
Run Code Online (Sandbox Code Playgroud)
要获得目标活动的输出,请尝试:
第二项活动.
Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("bmp_img");
Run Code Online (Sandbox Code Playgroud)
你总是使用getParcelableExtra("key")来获取Activity中传递的Bitmap.
| 归档时间: |
|
| 查看次数: |
21426 次 |
| 最近记录: |