【问题标题】:Saving the captured image in next activity在下一个活动中保存捕获的图像
【发布时间】:2012-10-25 19:33:41
【问题描述】:

我想保存并在下一个活动中显示捕获的图像。我已经编写了代码,但它无法正常工作我需要在该代码中进行哪些修改。

 camera.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("Coming in the camera intent","coming in yhe camera intent");
                    String filePath = Environment.getExternalStorageDirectory()+ "/s1.jpeg";
                    File file = new File(filePath);
                    Uri output = Uri.fromFile(file);
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
                }
            });
        }
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
                Log.d("Coming in the if loop","coming in the if loop");
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                Intent i1=new Intent(MyMenu.this,FullScreen.class);
                i1.putExtra("photooo",photo);
                startActivity(i1);
            }  
        } 

和接收我想要保存和显示图像的活动

image=(ImageView)findViewById(R.id.image);
        note=(ImageButton)findViewById(R.id.note);
        tick=(ImageButton)findViewById(R.id.tick);
        cross=(ImageButton)findViewById(R.id.cross);
        Intent intent = getIntent();
        Bitmap photo = (Bitmap) intent.getParcelableExtra("photooo");
        image.setImageBitmap(photo);
    }

堆栈

10-25 12:50:30.459: E/AndroidRuntime(27740): java.lang.RuntimeException: Unable to resume activity {com.example.babysnap/com.example.babysnap.MyMenu}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.babysnap/com.example.babysnap.MyMenu}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.babysnap/com.example.babysnap.FullScreen}; have you declared this activity in your AndroidManifest.xml?

【问题讨论】:

标签: android image camera sd-card


【解决方案1】:

您的FullScreen Activity 似乎没有在 AndroidManifest.xml 文件中声明。

检查一下。 并声明它。

【讨论】:

  • 谢谢,添加活动后工作正常,但图像保存在默认位置而不是指定路径
【解决方案2】:

首先将 Image 转换为字节数组,然后传入 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中获取字节数组并转换为位图图像:-

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

并在Androidmanifest.xml中写下声明Activity的代码。

<activity
    android:name=".FullScreen"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

【讨论】:

    【解决方案3】:

    尝试传递图像路径而不是位图。你已经得到了Uri output; 从此获取文件路径。

    String imagepath=output.getPath(); then intent.putExtra("imagepath",imagepath)
    

    在其他Activity中获取这个字符串:

    String path= getIntent().getStringExtra("imagepath"). then
    
    Bitmap myBitmap = BitmapFactory.decodeFil(path);    
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);
    

    Read here。你也可以使用字节数组来做到这一点

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多