嗨,我是碎片中的新手,所以请耐心等待。我试图从画廊或相机设置个人资料照片。代码正常运行"扩展活动"但是在Fragment中关闭了力量。
answer

Error Logcat output
12-14 10:14:11.112: E/AndroidRuntime(9687): FATAL EXCEPTION: main
12-14 10:14:11.112: E/AndroidRuntime(9687): Process: com.example.areal, PID: 9687
12-14 10:14:11.112: E/AndroidRuntime(9687): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131074, result=-1, data=Intent { dat=content://media/external/images/media/32375 flg=0x1 }} to activity {com.example.areal/com.example.areal.MainActivity}: java.lang.NullPointerException
12-14 10:14:11.112: E/AndroidRuntime(9687): at android.app.ActivityThread.deliverResults(ActivityThread.java:3591)
12-14 10:14:11.112: E/AndroidRuntime(9687): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3634)
12-14 10:14:11.112: E/AndroidRuntime(9687): at android.app.ActivityThread.access$1300(ActivityThread.java:155)
12-14 10:14:11.112: E/AndroidRuntime(9687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
12-14 10:14:11.112: E/AndroidRuntime(9687): at android.os.Handler.dispatchMessage(Handler.java:110)
12-14 10:14:11.112: E/AndroidRuntime(9687): at android.os.Looper.loop(Looper.java:193)
12-14 10:14:11.112: E/AndroidRuntime(9687): at android.app.ActivityThread.main(ActivityThread.java:5388)
12-14 10:14:11.112: E/AndroidRuntime(9687): at java.lang.reflect.Method.invokeNative(Native Method)
12-14 10:14:11.112: E/AndroidRuntime(9687): at java.lang.reflect.Method.invoke(Method.java:515)
12-14 10:14:11.112: E/AndroidRuntime(9687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.areal"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<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>
</application>
</manifest>
&#13;
答案 0 :(得分:0)
您还需要为外部存储添加“读取”权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
&#13;
现在让onActivityResult从gallery和camera获取位图。你需要在Activity类中放置onActivityResult,对于startActivity,你需要调用那个Activity引用,如。
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
activity.startActivityForResult(galleryIntent, 1);
&#13;
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
//requestCode=0 for Camera
if (requestCode == 0 && responseCode == RESULT_OK && data != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
if (bitmap != null) {
//Process with bitmap
}
//requestCode=1 for Gallery
} else if (requestCode == 1 && responseCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String path = cursor.getString(columnIndex);
//NewNewsFragment.fileURI = path;
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(path);
if (bitmap != null) {
//Process with bitmap
}
}
}
&#13;