我正在开发用于编辑照片的应用程序
我在第一个活动中有一个按钮,在第二个活动中有一个 ImageView。当我单击该按钮时,它将打开画廊,我将能够选择一个图像。所选图像需要出现在我的第二个活动中的 ImageView 中,但它没有。
下面是我的代码,它不起作用,你知道有什么问题吗?
第一项活动:
package com.example.odabirslike;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Pocetni extends Activity {
private Button buttonLoadImage;
private static final int SELECT_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pocetni);
this.buttonLoadImage = (Button) this.findViewById(R.id.pickbutton);
buttonLoadImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Complete action using"), SELECT_PICTURE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Bitmap selectedphoto = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String [] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
selectedphoto = BitmapFactory.decodeFile(filePath);
cursor.close();
Intent i = new Intent (Pocetni.this, Drugi.class);
i.putExtra("data",selectedphoto);
startActivity(i);
}
}
}
第二个活动:
package com.example.odabirslike;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.widget.ImageView;
public class Drugi extends Activity {
ImageView view = (ImageView) findViewById(R.id.imageView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drugi);
Bitmap selectedphoto =(Bitmap)this.getIntent().getParcelableExtra("data");
view.setImageBitmap(selectedphoto);
}
}
安卓清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.odabirslike"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.odabirslike.Pocetni"
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.example.odabirslike.Drugi"
android:label="Drugi" >
</activity>
</application>
</manifest>