android:bitmap decodeFile by filepath返回NullPointerException

时间:2014-10-04 07:32:38

标签: android file bitmap bitmapfactory

用户从图库中选择了图片后,图片在图片视图imgview中正确显示。代码如下:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == Activity.RESULT_OK) 
        {
            adapter.clear();
            single_path = data.getStringExtra("single_path");
            imageLoader.displayImage("file://" + single_path, imgview);
            image_picked = true;
        } 
    }

    public void Upload_new_image() 
    {
        if (image_picked == true)
        {
            String imgFilePath = "file://" + single_path;
            Bitmap bitmap = BitmapFactory.decodeFile(""+imgFilePath);       
            ByteArrayOutputStream stream = new ByteArrayOutputStream();        
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);     // Line 156: NPE
            byte[] image = stream.toByteArray();

            ParseFile file = new ParseFile("upload.png", image);
            file.saveInBackground();
            ParseObject imgupload = new ParseObject("photo_database");
            imgupload.saveInBackground();
            Constants.custom_toast(this, "Image uploaded", "");         
        }
        else
        {
            Constants.custom_toast(this, "No image is picked!", "");
        }

logcat的:

10-04 15:18:31.926: W/dalvikvm(20254): threadid=1: thread exiting with uncaught exception (group=0x41c77700)
10-04 15:18:31.931: E/AndroidRuntime(20254): FATAL EXCEPTION: main
10-04 15:18:31.931: E/AndroidRuntime(20254): java.lang.NullPointerException
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.abc.app.activity.User_pick_image.Upload_new_image(User_pick_image.java:156)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.abc.app.activity.User_pick_image.onOptionsItemSelected(User_pick_image.java:93)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.app.Activity.onMenuItemSelected(Activity.java:2640)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.view.View.performClick(View.java:4475)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.view.View$PerformClick.run(View.java:18786)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.os.Handler.handleCallback(Handler.java:730)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.os.Looper.loop(Looper.java:137)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at android.app.ActivityThread.main(ActivityThread.java:5493)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at java.lang.reflect.Method.invokeNative(Native Method)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at java.lang.reflect.Method.invoke(Method.java:525)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
10-04 15:18:31.931: E/AndroidRuntime(20254):    at dalvik.system.NativeStart.main(Native Method)

问题:

虽然图像可以通过imagerLoad.displayImage方法在imgview中正确显示,但是当按下通过Upload_new_image()方法上传图像时,无法获取到同一文件的相同文件链接,在logcat中出现错误。

信息:

我通过选择差异图像获得了single_path的值,完整值采用以下格式: /storage/emulated/0/Pictures/Screenshots/Screenshot_xxx.png

怎么可以解决?

非常感谢!

4 个答案:

答案 0 :(得分:1)

我不知道single_path的价值是多少。也许你应该添加" /"在imgFilePath之前:

Bitmap bitmap = BitmapFactory.decodeFile("/"+imgFilePath);

答案 1 :(得分:1)

 String imgFilePath = "file://" + single_path;

应该是:

 String imgFilePath = single_path;

答案 2 :(得分:0)

试试这个,从数据意图中获取Uri:

Uri contactUri = data.getData();

然后你应该从那个Uri那里得到你的绝对路径。

//编辑 这可能有所帮助:

public String getRealPathFromURI(Context context, Uri contentUri)
{
    Cursor cursor = null;
    try
    {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    finally
    {
        if (cursor != null)
        {
            cursor.close();
        }
    }
}

答案 3 :(得分:0)

另外可能有一个原因是没有添加运行时权限来读取外部存储空间,这可能会导致此类错误。所以,请检查此权限是否已通过应用信息中的设置授予。

注意: 您的应用的设置可能已禁用存储选项,需要启用。