我的應用程序工作編輯照片的Android如何打開圖像,並在第二個活動顯示它
我在第二個活動的第一項活動和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);
}
}
Android清單:
<?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>
我試着把這部分代碼,但仍然無法正常工作。 現在我上帝錯誤「源找不到」 – 2013-05-10 13:24:00
你可以請你發佈你的logcat?當你調用i.putExtra(「data」,filepath)時,filepath的內容是什麼? – DigCamara 2013-05-10 14:42:44
文件路徑內容應該從選擇的照片中取得。 問題從我從gallery中選擇照片時開始。我可以點擊一個按鈕,比我可以打開圖庫並選擇一些照片,然後調試器顯示該消息 我的logcat真的很大,我應該發佈一些特定的部分或問題? – 2013-05-11 13:27:04