簡體   English   中英

無法將完整尺寸的圖像從相機傳遞到下一個活動

[英]cant pass full size image from camera to the next activity

我想將圖像從一個活動傳遞到另一個活動,這是我的代碼:

 public boolean launchCamera(View view) {

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo;
        try {
            // place where to store camera taken picture
            photo = this.createTemporaryFile("picture", ".jpg");
            photo.delete();
        } catch (Exception e) {
            Log.v(TAG, "Can't create file to take picture!");
            Toast.makeText(MainActivity.this, "Please check SD card! Image shot is impossible!", Toast.LENGTH_LONG);
            return false;
        }
        mImageUri = Uri.fromFile(photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
        //start camera intent
        this.startActivityForResult(intent, MenuShootImage);
        return true;
    }

private File createTemporaryFile(String part, String ext) throws Exception {
        File tempDir = Environment.getExternalStorageDirectory();
        tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
        if (!tempDir.exists()) {
            tempDir.mkdir();
        }
        return File.createTempFile(part, ext, tempDir);
    }

 public Bitmap grabImage()
    {
        this.getContentResolver().notifyChange(mImageUri, null);
        ContentResolver cr = this.getContentResolver();
        Bitmap bitmap=null;
        try
        {
            bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);


        }
        catch (Exception e)
        {
            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
            Log.d(TAG, "Failed to load", e);
        }

        return bitmap;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
         Intent imagepass = null;
         Bitmap bitmap = null;
         if(requestCode == MenuShootImage && resultCode == RESULT_OK)
         {

             bitmap = this.grabImage();
             imagepass = new Intent(this,MainActivity2.class);
             imagepass.putExtra("imagepass", bitmap );
             startActivity(imagepass);
         }

    }

問題是我根本無法達到其他活動,在調試模式下,我進入了startactivity(imagepass)行; 不要去MainActivity2。 有人可以幫我嗎?

首先,如果您真的想通過Intent傳遞位圖,則需要先將其轉換為Byte Array,因為不能用這樣的Intent附加位圖。

這是如何做的https://stackoverflow.com/a/11010565/4651112

但是,根據最佳實踐,我建議您完全不要通過意圖發送位圖 發送文件名圖像,並讓目標Activity從文件中對其進行解碼。 好多了

1)首先將圖像轉換為字節數組,然后傳遞到Intent,然后在下一個活動中從Bundle中獲取字節數組,然后轉換為Image(Bitmap)並設置為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();

將字節數組傳遞給意圖:

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

從捆綁中獲取字節數組並轉換為位圖圖像:-

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);

2)首先將圖像保存到SDCard,然后在下一個活動中將此圖像設置為ImageView。

3)將位圖傳遞到Intent並從包中的下一個活動中獲取位圖,但是問題是,如果那時您的位圖/圖像大小很大,則無法在下一個活動中加載圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM