来自片段类的getApplicationContext()

时间:2014-03-14 03:41:52

标签: java android

我正在尝试从位图对象中获取所选图像的路径,以便我可以将其上传到服务器。但是在调用方法getImageUri()

时我得到java.lang.NullPointerException:uriString
 if (requestCode == PICK_FROM_GALLERY) {
                Bundle extras2 = data.getExtras();
                if (extras2 != null) {
                    Bitmap photo = extras2.getParcelable("data");
                    try{
                    Uri path=getImageUri(getActivity().getApplicationContext(), photo);
                    }catch(Exception e){
                        Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
                    }
                    //uploadFile(getRealPathFromURI(path));
                    group.setImageBitmap(photo);
                }
            }
    public Uri getImageUri(Context inContext, Bitmap inImage) {
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
          String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
          return Uri.parse(path);
        }

[UPDATE1]

logcat的

03-14 09:18:47.782: W/IInputConnectionWrapper(30669): showStatusIcon on inactive InputConnection
03-14 09:18:47.847: D/OpenGLRenderer(30669): Flushing caches (mode 0)
03-14 09:18:47.852: D/ActivityThread(30669): ACT-DESTROY_ACTIVITY handled : 1 / android.os.BinderProxy@41956178
03-14 09:18:47.854: D/OpenGLRenderer(30669): Flushing caches (mode 0)
03-14 09:18:47.855: D/OpenGLRenderer(30669): Flushing caches (mode 0)
03-14 09:18:47.855: D/OpenGLRenderer(30669): Flushing caches (mode 2)
03-14 09:18:55.910: E/Trace(30912): error opening trace file: No such file or directory (2)
03-14 09:18:55.911: D/jdwp(30912): sendBufferedRequest : len=0x3F
03-14 09:18:55.950: D/dalvikvm(30912): open_cached_dex_file : /data/app/com.letsmeet.android-1.apk /data/dalvik-cache/data@app@com.letsmeet.android-1.apk@classes.dex
03-14 09:18:55.957: D/ActivityThread(30912): BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{416edd38 com.letsmeet.android}}
03-14 09:18:56.016: I/MdpService(30912): [MDP INFO](30912): BpMdpService::parseJpg addr:0x51ff0000, size:65536, fd:43
03-14 09:18:56.017: I/MdpService(30912): [MDP INFO](30912): BpMdpService::parseJpg reply:1
03-14 09:18:56.017: W/skia(30912): Use JPEG SW Decoder
03-14 09:18:56.050: D/dalvikvm(30912): GC_FOR_ALLOC freed 102K, 9% free 9174K/10035K, paused 14ms, total 14ms
03-14 09:18:56.053: I/dalvikvm-heap(30912): Grow heap (frag case) to 10.604MB for 1536016-byte allocation
03-14 09:18:56.069: D/skia(30912): jpeg_decoder finish successfully, L:1243, reuse 0!!!
03-14 09:18:56.070: D/dalvikvm(30912): GC_FOR_ALLOC freed <1K, 8% free 10673K/11575K, paused 16ms, total 16ms
03-14 09:18:56.107: D/dalvikvm(30912): GC_CONCURRENT freed <1K, 8% free 10677K/11575K, paused 12ms+12ms, total 37ms
03-14 09:18:56.167: E/MediaStore(30912): Failed to insert image
03-14 09:18:56.167: E/MediaStore(30912): java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=30912, uid=10091 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
03-14 09:18:56.167: E/MediaStore(30912):    at android.os.Parcel.readException(Parcel.java:1425)
03-14 09:18:56.167: E/MediaStore(30912):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
03-14 09:18:56.167: E/MediaStore(30912):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
03-14 09:18:56.167: E/MediaStore(30912):    at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
03-14 09:18:56.167: E/MediaStore(30912):    at android.content.ContentResolver.insert(ContentResolver.java:888)
03-14 09:18:56.167: E/MediaStore(30912):    at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:899)
03-14 09:18:56.167: E/MediaStore(30912):    at com.letsmeet.android.FragmentCreateGroup.getImageUri(FragmentCreateGroup.java:118)
03-14 09:18:56.167: E/MediaStore(30912):    at com.letsmeet.android.FragmentCreateGroup.onActivityResult(FragmentCreateGroup.java:91)
03-14 09:18:56.167: E/MediaStore(30912):    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:166)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.Activity.dispatchActivityResult(Activity.java:5204)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3298)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2715)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2760)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
03-14 09:18:56.167: E/MediaStore(30912):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 09:18:56.167: E/MediaStore(30912):    at android.os.Looper.loop(Looper.java:153)
03-14 09:18:56.167: E/MediaStore(30912):    at android.app.ActivityThread.main(ActivityThread.java:5086)
03-14 09:18:56.167: E/MediaStore(30912):    at java.lang.reflect.Method.invokeNative(Native Method)
03-14 09:18:56.167: E/MediaStore(30912):    at java.lang.reflect.Method.invoke(Method.java:511)
03-14 09:18:56.167: E/MediaStore(30912):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-14 09:18:56.167: E/MediaStore(30912):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-14 09:18:56.167: E/MediaStore(30912):    at dalvik.system.NativeStart.main(Native Method)
03-14 09:18:56.183: D/ActivityThread(30912): ACT-AM_ON_RESUME_CALLED ActivityRecord{416ef1c8 token=android.os.BinderProxy@416ee8a8 {com.letsmeet.android/com.letsmeet.android.User}}
03-14 09:18:56.190: D/ActivityThread(30912): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{416ef1c8 token=android.os.BinderProxy@416ee8a8 {com.letsmeet.android/com.letsmeet.android.User}}
03-14 09:18:56.202: V/InputMethodManager(30912): Not IME target window, ignoring
03-14 09:18:56.272: I/SurfaceTextureClient(30912): [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
03-14 09:18:56.277: D/libEGL(30912): loaded /vendor/lib/egl/libEGL_mtk.so
03-14 09:18:56.281: D/libEGL(30912): loaded /vendor/lib/egl/libGLESv1_CM_mtk.so
03-14 09:18:56.284: D/libEGL(30912): loaded /vendor/lib/egl/libGLESv2_mtk.so
03-14 09:18:56.325: D/OpenGLRenderer(30912): Enabling debug mode 0
03-14 09:18:56.343: I/SurfaceTextureClient(30912): [void android::SurfaceTextureClient::init()] debug.stc.fps: 3000 ms
03-14 09:18:56.345: V/InputMethodManager(30912): onWindowFocus: android.widget.EditText@41702760 softInputMode=32 first=true flags=#1810100
03-14 09:18:56.345: V/InputMethodManager(30912): Starting input: view=android.widget.EditText@41702760
03-14 09:18:56.346: V/InputMethodManager(30912): Starting input: tba=android.view.inputmethod.EditorInfo@418ecb70 ic=com.android.internal.widget.EditableInputConnection@418ecf90
03-14 09:18:56.346: V/InputMethodManager(30912): START INPUT: android.widget.EditText@41702760 ic=com.android.internal.widget.EditableInputConnection@418ecf90 tba=android.view.inputmethod.EditorInfo@418ecb70 controlFlags=#107
03-14 09:18:56.349: V/InputMethodManager(30912): Starting input: Bind result=InputBindResult{null com.android.inputmethod.latin/.LatinIME #1657}
03-14 09:18:56.428: V/InputMethodManager(30912): Starting input: view=android.widget.EditText@41702760
03-14 09:18:56.429: V/InputMethodManager(30912): Starting input: tba=android.view.inputmethod.EditorInfo@418f1140 ic=com.android.internal.widget.EditableInputConnection@418f1188
03-14 09:18:59.861: I/SurfaceTextureClient(30912): [0x515ec900] frames:8, duration:3.464000, fps:2.309199
03-14 09:19:02.878: I/SurfaceTextureClient(30912): [0x515ec900] frames:6, duration:3.016000, fps:1.988820
03-14 09:19:06.375: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.497000, fps:2.001632
03-14 09:19:09.873: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.498000, fps:2.000954
03-14 09:19:12.890: I/SurfaceTextureClient(30912): [0x515ec900] frames:6, duration:3.016000, fps:1.988986
03-14 09:19:15.890: I/SurfaceTextureClient(30912): [0x515ec900] frames:6, duration:3.000000, fps:1.999910
03-14 09:19:18.905: I/SurfaceTextureClient(30912): [0x515ec900] frames:6, duration:3.015000, fps:1.989759
03-14 09:19:22.403: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.497000, fps:2.001223
03-14 09:19:25.904: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.500000, fps:1.999735
03-14 09:19:28.920: I/SurfaceTextureClient(30912): [0x515ec900] frames:6, duration:3.016000, fps:1.989333
03-14 09:19:32.418: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.498000, fps:2.000993
03-14 09:19:35.433: I/SurfaceTextureClient(30912): [0x515ec900] frames:6, duration:3.015000, fps:1.989715
03-14 09:19:38.941: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.507000, fps:1.995743
03-14 09:19:41.949: I/SurfaceTextureClient(30912): [0x515ec900] frames:6, duration:3.007000, fps:1.994813
03-14 09:19:45.447: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.498000, fps:2.000985
03-14 09:19:48.945: I/SurfaceTextureClient(30912): [0x515ec900] frames:7, duration:3.497000, fps:2.001242

为什么我得到这个例外?

1 个答案:

答案 0 :(得分:4)

你的logcat清楚地说了

 2): Failed to insert image
03-14 09:18:56.167: E/MediaStore(30912): java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=30912, uid=10091 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

您需要在manifest.xml文件中添加以下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />