Android ||如何将图像从一个活动解析到另一个活动?

时间:2017-01-23 07:50:21

标签: android parsing

这是我的代码。我想在这里添加一个按钮,onclick将图像发送到下一个活动,但我无法配置它。我该怎么办?

public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void loadImagefromGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            ImageView imgView = (ImageView) findViewById(R.id.imgView);
            // Set the Image in ImageView after decoding the String
            imgView.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

}

4 个答案:

答案 0 :(得分:0)

您可以将数据放在第一个活动中,例如

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

并在第二个活动中解析如下数据:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

将你的imagepath转换为剪辑的位图代码:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

答案 1 :(得分:0)

使用大图像的最佳方法是使用Serializable,因为Intent和Parcelable都有大小限制。

此问题的答案可能有所帮助:Serializing and De-Serializing android.graphics.Bitmap in Java

答案 2 :(得分:0)

最初,在获取图像字符串启用按钮并将onActivityResult设置为按钮后,在onClickListener方法中保持新添加的按钮禁用。并使用intent extra将该图像字符串传递给下一个活动。

onActivityResult方法中,

newButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),NextActivity.class).putExtra("img",imgDecodableString));
            }
        });

答案 3 :(得分:0)

//第一项活动

Bitmap b = null;
String bitmapString = getStringFromBitmap(b);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("bitmapString", bitmapString);

//第二项活动

String receivedBitmapString = getIntent().getStringExtra("bitmapString");
        Bitmap receivedBitmap = getBitmapFromString(receivedBitmapString);

//功能

 private String getStringFromBitmap(Bitmap bitmapPicture) {
     /*
     * This functions converts Bitmap picture to a string which can be
     * JSONified.
     * */
     final int COMPRESSION_QUALITY = 100;
     String encodedImage;
     ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
     bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
     byteArrayBitmapStream);
     byte[] b = byteArrayBitmapStream.toByteArray();
     encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
     return encodedImage;
     }


    private Bitmap getBitmapFromString(String jsonString) {
    /*
    * This Function converts the String back to Bitmap
    * */
    byte[] decodedString = Base64.decode(stringPicture, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    return decodedByte;
    }

来源:Link