1

我有两个活动第一个活动具有动态图像视图,单击时我正在调用第二个活动,其中调用相机以捕获图像,现在我可以调用相机并捕获图像但是我如何将该图像传递给前一个活动并设置为图像视图。

String fileName = "testphoto.jpg";
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, fileName);
    values.put(MediaStore.Images.Media.DESCRIPTION,
            "Image capture by camera");
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    imageUri = getContentResolver().insert(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, IMAGE_CAPTURE);
4

3 回答 3

1

似乎您正在尝试从相机捕获图像并设置您的视图,但我认为您不需要两个活动来实现您的目标。

您可以借助startActivityForResult.

只需Camera Intent点击 imageview,并在同一活动中取回您的结果,请检查以下代码:

定义捕获图像的路径:

String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+filename;
System.out.println("thumbnail path~~~~~~"+filepath);
File file = new File(filepath);
Uri outputFileUri = Uri.fromFile(file);

单击imageview对此进行编码:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 100);

onActivityResult 在同一活动中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
        Bitmap bitmap;
        bitmap=GlobalMethods.decodeFile(_path);//function to decode.
        if (bitmap == null) {
            ivPhy.setImageBitmap(bitmap);
        } 
        else {
            ivPhy.setImageBitmap(bitmap);
            ivPhy.setScaleType(ScaleType.FIT_XY);
        }
    }


}

所需的权限传递到清单文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
于 2013-01-24T05:23:23.613 回答
1

最后,我在startActivityForResult.

检查下面的代码,希望对您有所帮助:

主要的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >



    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="57dp"
        android:layout_marginTop="84dp"
        android:text="Button" />


     <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
         />

</RelativeLayout>

第一活动.java

它将打开第二个活动startActivityResult

package com.example.demo_cameraintent;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity {
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image=(ImageView)findViewById(R.id.image);

        String extStorageDirectory = Environment.getExternalStorageDirectory()
                + "/testing";

      File xmlDirectory = new File(extStorageDirectory);
        if (!xmlDirectory.exists())
            xmlDirectory.mkdirs();

        Button btn=(Button)findViewById(R.id.button1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(i, 1);

            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {

             if(resultCode == RESULT_OK){

              String result=data.getStringExtra("result");
              Log.d("*****************", "inside onactivityresult in main activity="+result);

            Bitmap bitmap = BitmapFactory.decodeFile(result);
            image.setImageBitmap(bitmap);
            image.setScaleType(ScaleType.FIT_XY);

        }

        if (resultCode == RESULT_CANCELED) {



             //Write your code on no result return 

        }
        }//onAcrivityResult


    }
}

SecondActivity.java

在这里,当它被调用时,它将直接打开cameraIntent并在onActivityResult其中返回 Firstactivity 并输出您的输出

package com.example.demo_cameraintent;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;

public class SecondActivity extends Activity {
    String filepath;

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

         filepath = Environment.getExternalStorageDirectory()+"/testing/"+"test.jpg";
        System.out.println("thumbnail path~~~~~~"+filepath);
        File file = new File(filepath);
        Uri outputFileUri = Uri.fromFile(file);


        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 100);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 100) {

                 Intent returnIntent = new Intent();
                 returnIntent.putExtra("result",filepath);
                 setResult(RESULT_OK,returnIntent);     
                 finish();
        }


    }



}

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo_cameraintent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demo_cameraintent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="SecondActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />

</manifest>
于 2013-01-24T06:39:43.277 回答
0

而不是ContentValues用于创建imageUri,只需将您的图像存储在 sdcard 中,如下所示:

String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/testphoto.jpg";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)) ); 

之后,在调用第二个活动时,只需像这样传递文件的路径:

intent.putExtra("pathOfImage",path);

现在,在第二个活动中,您可以从 sdcard 中检索图像:

File f = new File(getIntent().getStringExtra("pathOfImage"));
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);
于 2013-01-24T05:57:32.107 回答