2015-06-04 25 views
2

我有一个应用程序,它具有允许用户从图库中选择图像的功能。下面是这样做的代码:从Nexus 5(Android 5.1.1)上的图库中选取图像返回null,在其他设备上工作

@OnClick(R.id.imageView_fragmentUploadLayout_uploadedArt) 
public void setUploadedArtImageView() { 
    //startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY); 
    Intent intent = new Intent(); 
// Show only images, no videos or anything else 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
// Always show the chooser (if there are multiple options available) 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), GET_FROM_GALLERY); 

} 

而且onActivityResult方法:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    //Detects request codes 
    if(requestCode == GET_FROM_GALLERY && resultCode == Activity.RESULT_OK && data != null) { 
     selectedImageUri = data.getData(); 
     selectedImagePath = getPath(selectedImageUri); 

     file = new File(selectedImagePath); 

     //ParseFile parseImage = new ParseFile(); 

     Picasso.with(getActivity()) 
       .load(selectedImageUri.toString()) 
       .resize(500, 500) 
       .centerCrop() 
       .noPlaceholder() 
       .into(uploadedArtImageView); 

     uploadedArtImageViewPlaceHolder.setVisibility(View.GONE); 

     ImageResizer.resize(file, 640, 480, ResizeMode.FIT_EXACT); 
    } 
} 

的getPath方法:

public String getPath(Uri uri) { 
    // just some safety built in 
    if(uri == null) { 
     // TODO perform some logging or show user feedback 
     return null; 
    } 
    // try to retrieve the image from the media store first 
    // this will only work for images selected from gallery 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null); 
    if(cursor != null){ 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    // this is our fallback here 
    return uri.getPath(); 
} 

如果我测试它在我的三星S2它的工作原理,(目前在Cyanogen Mod,Android 4.0.4上),但如果我在Nexus 5(Android 5.1.1)上测试它,它会返回空指针异常。

注意

我尝试了多种不同的应用程序:我认为,从与谷歌照片画廊获得的图片可能引起的问题,但我安装了它在我的S2和它的作品。

这里是堆栈跟踪:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131075, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:25147 flg=0x1 }} to activity {com.entu.artapp/com.entu.artapp.activity.FragmentManager}: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3574) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
    at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference 
    at java.io.File.fixSlashes(File.java:185) 
    at java.io.File.<init>(File.java:134) 
    at com.entu.artapp.fragments.UploadPictureFragment.onActivityResult(UploadPictureFragment.java:363) 
    at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163) 
    at android.app.Activity.dispatchActivityResult(Activity.java:6192) 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3570) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
    at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

你能帮我解决这个问题?

在此先感谢,欢呼!

+1

检查。 http://stackoverflow.com/a/30576188 – siriscac

回答

1

A Uri is not a file。您无法可靠地获得用户通过ACTION_GET_CONTENT选择的某个本地路径。您需要将Uri视为到Web服务器的URL,您可以直接在Uri上打开流,无论是直接(getContentResolver().openInputStream())还是间接(通过Picasso)。

我不知道什么是ImageResizer,但如果只用文件的工作,停止使用。删除您的getPath()方法,仅使用Uri

+0

我已经做了你所问,它不会崩溃了。该图像加载在profilePictureImageView上,但是当我保存它时,它不保存在服务器上。在S2上它可以工作,但在Nexus 5上却不行。 –

+0

@ VladIancu:“但是当我保存它,它不会保存在服务器上” - 那么你将需要打开一个新的堆栈溢出问题,在那里你提供你的代码和确切的症状(即什么“不保存在服务器上“的意思)。 – CommonsWare

+0

我解决了它。感谢您的帮助和关注! –

1

而是传递字符串从意向直接从onActivityResult

将URI传递MainActivity.class

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
// TODO Auto-generated method stub 
super.onActivityResult(requestCode, resultCode, data); 

    Uri imageuri=data.getData(); 

Intent i=new Intent(MainActivity.this,NextActivity.class); 
i.putExtra("uri","imageuri"); 
startActivity(i); 
} 

NextActivtiy.class

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_next); 

    Uri geturi=getIntent().getParcelableExtra("imageuri"); 
    InputStream is=null; 
    try { 
     is = getContentResolver().openInputStream(geturi); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Bitmap bitmap=BitmapFactory.decodeStream(is); 
    //Use this bitmap where you want to display the image 

} 
+0

我解决了!不管怎么说,还是要谢谢你! –

+0

@Vlad Iancu,你能否在这里提供你的工作解决方案。 – Venugopal

相关问题