cursor.getString返回带有有效uri的null

乔什·布特

程序突然停止工作了。

我有一个URI:“ content://com.android.providers.media.documents/document/image%3A13”,它是图像的文件路径。

URI的路径选择如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode,  data);
        if(resultCode == RESULT_OK) {
            try {

                // This bit here
                Bitmap bitmap = getPath(data.getData());
                Log.i("Bitmap", "Bmp: " + data.getData());

            }catch (Exception e){
                Log.e("Error", "Error with setting the image.");
                e.printStackTrace();
            }
        }
    }

因此getPath()称为,将数据作为URI放入(URI是正确的,日志显示)

getPath()

private Bitmap getPath(Uri uri) {

        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null,null);

        cursor.moveToFirst();

        // it is this line here, it returns null for some reason.
        String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

        // Convert file path into bitmap image using below line.

        Log.i("File Path", "File name: " + filePath); // this comes out as NULL in the logcat

        Bitmap bitmap = BitmapFactory.decodeFile(filePath);

        filePathForUpload = filePath;

        try {
            ExifInterface exif = new ExifInterface(filePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
            bitmap = rotateBitmap(bitmap, orientation);
        }catch (Exception e){
            Log.d("Error", "error with bitmap!");
            e.printStackTrace();
        }

        return bitmap;
    }

Logcat输出:

 java.lang.IllegalArgumentException: filename cannot be null
     at android.media.ExifInterface.<init>(ExifInterface.java:121)
     at build.com.build.SubmitPicActivity.getPath(SubmitPicActivity.java:123)
     at build.com.build.SubmitPicActivity.onActivityResult(SubmitPicActivity.java:93)
     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5017)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
     at dalvik.system.NativeStart.main(Native Method)

第123行是: String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

93行是: Bitmap bitmap = getPath(data.getData());

有什么建议?

常用软件

AUri不一定是File您正在使用,试图得到一个代码FileUri从未可靠,不会工作变得前进。

请消耗Uri适当,使用ContentResolver和方法,如openInputStream()getType()本质上,您对待UriWeb服务器的URL的方式相同,并且出于相同的原因:对于a浮出水面的内容ContentProvider不需要来自File您的应用可以访问的普通内容

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

cursor.getString返回带有有效uri的null

来自分类Dev

使用带有 uri 的 Canvas Cursor css 属性?

来自分类Dev

带有cursor.getInt()的CursorIndexOutOfBoundsException

来自分类Dev

MongoDB Cursor-计数和枚举文档的有效方法(在Node中)

来自分类Dev

MongoDB Cursor-计数和枚举文档的有效方法(在Node中)

来自分类Dev

当我尝试使用改造 2 上传图像时,我的 cursor.getString() 返回 null

来自分类Dev

CURSOR FOR LOOP 返回的查询数据

来自分类Dev

cursor.moveToFirst() 返回 false

来自分类Dev

带有PostgreSQL和REF_CURSOR的JPA 2.1 StoredProcedureQuery

来自分类Dev

在select语句中带有变量的MySQL Cursor

来自分类Dev

有效的api请求返回null

来自分类Dev

Mongo的Cursor.nextObject有时错误地返回Null吗?

来自分类Dev

Mongo的Cursor.nextObject有时错误地返回Null吗?

来自分类Dev

如果特定列的所有行都为空,则 Cursor.getType() 返回 FIELD_TYPE_NULL

来自分类Dev

收到带有curl的数据后,Json_decode使用有效的JSON返回NULL

来自分类Dev

json_decode返回带有有效json的NULL

来自分类Dev

psycopg中的“ cursor”类有什么意义?

来自分类Dev

没有moveToFirst的Cursor.moveToNext

来自分类Dev

具有多个$ {cursor}的Eclipse XML模板?

来自分类Dev

AttributeError:“ SnowflakeCursor”对象没有属性“ cursor”

来自分类Dev

NSFileHandle fileHandleForWritingAtPath返回nil并带有有效路径

来自分类Dev

NSJSONSerialization返回nil并带有有效的json

来自分类Dev

确保URI有效

来自分类Dev

Python cursor.fetchall()返回空元组

来自分类Dev

cursor.moveToFirst())不返回true

来自分类Dev

Phonegap FileReader readAsText返回null,但readAsDataURL有效

来自分类Dev

json_decode使用有效json返回NULL

来自分类Dev

必须返回有效的React元素(或null)

来自分类Dev

必须返回有效的 react 元素或 null -- Reactjs 错误

Related 相关文章

  1. 1

    cursor.getString返回带有有效uri的null

  2. 2

    使用带有 uri 的 Canvas Cursor css 属性?

  3. 3

    带有cursor.getInt()的CursorIndexOutOfBoundsException

  4. 4

    MongoDB Cursor-计数和枚举文档的有效方法(在Node中)

  5. 5

    MongoDB Cursor-计数和枚举文档的有效方法(在Node中)

  6. 6

    当我尝试使用改造 2 上传图像时,我的 cursor.getString() 返回 null

  7. 7

    CURSOR FOR LOOP 返回的查询数据

  8. 8

    cursor.moveToFirst() 返回 false

  9. 9

    带有PostgreSQL和REF_CURSOR的JPA 2.1 StoredProcedureQuery

  10. 10

    在select语句中带有变量的MySQL Cursor

  11. 11

    有效的api请求返回null

  12. 12

    Mongo的Cursor.nextObject有时错误地返回Null吗?

  13. 13

    Mongo的Cursor.nextObject有时错误地返回Null吗?

  14. 14

    如果特定列的所有行都为空,则 Cursor.getType() 返回 FIELD_TYPE_NULL

  15. 15

    收到带有curl的数据后,Json_decode使用有效的JSON返回NULL

  16. 16

    json_decode返回带有有效json的NULL

  17. 17

    psycopg中的“ cursor”类有什么意义?

  18. 18

    没有moveToFirst的Cursor.moveToNext

  19. 19

    具有多个$ {cursor}的Eclipse XML模板?

  20. 20

    AttributeError:“ SnowflakeCursor”对象没有属性“ cursor”

  21. 21

    NSFileHandle fileHandleForWritingAtPath返回nil并带有有效路径

  22. 22

    NSJSONSerialization返回nil并带有有效的json

  23. 23

    确保URI有效

  24. 24

    Python cursor.fetchall()返回空元组

  25. 25

    cursor.moveToFirst())不返回true

  26. 26

    Phonegap FileReader readAsText返回null,但readAsDataURL有效

  27. 27

    json_decode使用有效json返回NULL

  28. 28

    必须返回有效的React元素(或null)

  29. 29

    必须返回有效的 react 元素或 null -- Reactjs 错误

热门标签

归档