我有两个活动第一个活动有动态图像视图,点击我调用第二个活动,其中调用相机来捕获图像,现在我能够调用相机并捕获图像,但我将如何传递该图像到上一个活动并设置为图像视图。
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);
答案 0 :(得分:1)
似乎你试图从相机中捕捉图像并设置你的视图,但我认为你不需要两个活动来实现你的目标。
您可以在startActivityForResult
的帮助下实现单一活动。
只需点击图片查看即可致电Camera Intent
,并在同一活动中取回您的结果,请查看以下代码:
定义捕获图像的路径:
String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+filename;
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);
同一活动中的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" />
答案 1 :(得分:1)
最后,我在startActivityForResult
的帮助下实现了两项活动。
检查下面的代码,希望对您有所帮助:
<强> main.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>
<强> FirstActivity.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();
}
}
}
<强>的Manifest.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>
答案 2 :(得分:0)
而不是使用ContentValues
创建imageUri
,只需将您的图片存储在SD卡中,如下所示:
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);