我是android的新手,现在我还在构建一个包含两个活动的应用程序首先是LoginPage,它运行良好,第二个是UserPage问题从这里开始。我在UserPage上创建了两个ImageView,我想要的是
当我单击第一个ImageView并从相机保存图片时效果很好,当我点击第二个图像视图时出现问题,当我拍照并将其保存到第二个图像视图时,第一个图像视图中的图片消失了
这是我的代码: user.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerInside" />
<ImageView
android:id="@+id/image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@id/image1"
android:layout_alignTop="@id/image1"
android:scaleType="centerInside" />
</RelativeLayout>
UserPage.java
public class UserPage extends Activity {
ImageView imageView1, imageView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user);
imageView1 = (ImageView)findViewById(R.id.image1);
imageView1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 1);
}
});
imageView2 = (ImageView)findViewById(R.id.image2);
imageView2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent ii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(ii, 2);
}
});
}//OnCreate
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
Bitmap userImage = (Bitmap)data.getExtras().get("data");
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
userImage = (Bitmap) extras.get("data");
imageView1.setImageBitmap(userImage);
}//if resultCode Case 1
break;
case 2:
if(resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
userImage = (Bitmap) extras.get("data");
imageView2.setImageBitmap(userImage);
}//if resultCode Case 2
}//Switch
}//onActivityResult
}//UserPage
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pramana.report"
android:versionCode="2"
android:versionName="1.1" >
<uses-sdk
android:minSdkVersion="8"
/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.camera"></uses-feature>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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="com.pramana.report.UserPage"></activity>
</application>
</manifest>
请帮助,我已经搜索并尝试了一些建议 Take multiple images with camera and insert into multiple diffefent imageViews 但它没有用
抱歉我的英语不好我的英语不太好
答案 0 :(得分:0)
尝试这个工作:
全球声明
ImageView iv1 ,iv2;
private static Bitmap userImage1,userImage2;
点击两个Imageview的监听器
iv1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 1);
}
});
iv2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 2);
}
});
onActivityResult代码
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:
if(resultCode == RESULT_OK){
userImage1 = (Bitmap)data.getExtras().get("data");
Bundle extras = data.getExtras();
userImage1 = (Bitmap) extras.get("data");
iv1.setImageBitmap(userImage1);
}//if resultCode Case 1
break;
case 2:
if(resultCode == RESULT_OK) {
userImage2 = (Bitmap)data.getExtras().get("data");
Bundle extras = data.getExtras();
userImage2 = (Bitmap) extras.get("data");
iv2.setImageBitmap(userImage2);
}//if resultCode Case 2
}//Switch
}//onActivityResult
答案 1 :(得分:0)
尝试这个:
private static Bitmap sImageView1 = null;
私有静态位图sImageView2 = null;
private static Uri image_uri;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SavedInstanceState mSavedInstanceState = savedInstanceState;
if (mSavedInstanceState==null){
sImageView1=null;
sImageView2=null;
}
imageView1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
image_uri = fileUri;
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, 1);
}
});
}
@Override
public void onStart() {
super.onStart();
if (sImageView1 !=null) {
imageView1.setImageBitmap(userImage);
}
if (sImageView2 !=null) {
imageView2.setImageBitmap(userImage);
}
}
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
//UPDATE THIS CODE
switch(requestCode){
case 1:
if(resultCode == RESULT_OK){
sImageView1=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
//SAVE YOUR IMAGE IN A SHARED PREFERENCES OR IN ANY LOCAL DATABASE
RotateBitmap(sImageView1, 0);
//imageView1.setImageBitmap(sImageView1);
}//if resultCode Case 1
break;
}//Switch
}
public Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
sImageView1 = source;
sImageView1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
imageView1.setImageBitmap(sImageView1);
imageView1.setAdjustViewBounds(true);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
答案 2 :(得分:0)
感谢Sherin和Yograj Shinde,解决方案是我必须在开始新活动之前保存活动。这是我的代码:
UserPage.Java
public class UserPage extends Activity {
ImageView iv1 ,iv2;
private static Bitmap userImage1,userImage2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user);
if(savedInstanceState != null)
{
userImage1 = savedInstanceState.getParcelable("userImage1");
}
iv1 = (ImageView)findViewById(R.id.image1);
iv2 = (ImageView)findViewById(R.id.image2);
iv1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 1);
}
});
iv2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 2);
}
});
}
@Override
public void onStart() {
super.onStart();
if(userImage1 != null)
{
iv1.setImageBitmap(userImage1);
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelable("userImage1", userImage1);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:
if(resultCode == RESULT_OK){
userImage1 = (Bitmap)data.getExtras().get("data");
Bundle extras = data.getExtras();
userImage1 = (Bitmap) extras.get("data");
iv1.setImageBitmap(userImage1);
}//if resultCode Case 1
break;
case 2:
if(resultCode == RESULT_OK) {
userImage2 = (Bitmap)data.getExtras().get("data");
Bundle extras = data.getExtras();
userImage2 = (Bitmap) extras.get("data");
iv2.setImageBitmap(userImage2);
}//if resultCode Case 2
}//Switch
}//onActivityResult
}//Class UserPage
这里的想法是,当您开始新活动时,您必须保存上一个活动以防止丢失。这就是我使用
的原因@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelable("userImage1", userImage1);
}
在开始新活动之前保存数据,并在新活动开始时使用onStart()设置ImageView 本文将帮助您理解:
http://developer.android.com/reference/android/app/Activity.html
How to use onSavedInstanceState example please
注意: 该计划仍然需要很多改进,因为它还没有完成,但我希望它能帮助其他人