简体   繁体   English

在Android中从图像中获取uri

[英]Get uri from image in Android

I am trying to get the Path from an image to send it later to a server.我正在尝试从图像中获取路径,以便稍后将其发送到服务器。 The problem is when I try to get it, my code doesn't work (you will see there is an extra } . That is because the code from the OnCreate ends and then I worte the other functions):问题是当我尝试获取它时,我的代码不起作用(您会看到有一个额外的} 。那是因为来自 OnCreate 的代码结束,然后我编写了其他函数):

enviar.setOnClickListener(new View.OnClickListener() {
            String datos="";
            //Bundle extras=getIntent().getExtras();
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 0);
                //Uri imagen=intent.getData();
                //datos=imagen.getPath();
                //mostrar.setText(datos);
            }
        });
    }

    private String getRealPathFromURI(Uri contentURI) {
        String result;
        Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
        if (cursor == null) {
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch(requestCode) {
            case 0:
                if(resultCode == this.RESULT_OK){
                    try {
                        final Uri imageUri = imageReturnedIntent.getData();
                        String path = getRealPathFromURI(imageUri);
                        mostrar.setText(path);
                    }
                    catch (Exception e) {
                        Log.e("Erroreeeee: ", e.getMessage());
                    }
                }
                break;
        }
    }

You have two problems.你有两个问题。

The first one is that startActivityForResult() is not immediate.第一个是startActivityForResult()不是立即的。 You do not have any results in the next statement.您在下一个语句中没有任何结果。

So, you are welcome to call startActivityForResult() , as I do in this sample app :因此,欢迎您调用startActivityForResult() ,就像我在此示例应用程序中所做的那样:

private void get() {
    Intent i=
      new Intent()
        .setType("image/png")
        .setAction(Intent.ACTION_GET_CONTENT)
        .addCategory(Intent.CATEGORY_OPENABLE);

    startActivityForResult(i, REQUEST_GET);
}

Your results are delivered to onActivityResult() :您的结果将传送到onActivityResult()

  @Override
  public void onActivityResult(int requestCode, int resultCode,
                               Intent resultData) {
    if (resultCode==Activity.RESULT_OK) {
      Uri image=resultData.getData();
      // do something
    }
  }

Your second problem is that you are thinking that you are picking a file.您的第二个问题是您认为您正在选择一个文件。 You are not.你不是。 You are picking a piece of content, using any activity that the user decides to have handle ACTION_GET_CONTENT .您正在选择一段内容,使用用户决定处理ACTION_GET_CONTENT任何活动。 getPath() only has meaning if the scheme of the Uri is file , and that is rather unlikely. getPath()仅在Uri的方案是file时才有意义,而这是不太可能的。 There is no reliable means of getting a filesystem path for an arbitrary Uri , for the simple reason that the Uri does not have to point to a file.没有可靠的方法来获取任意Uri的文件系统路径,原因很简单, Uri不必指向文件。

Ideally, your "upload to a server" logic can work with an InputStream .理想情况下,您的“上传到服务器”逻辑可以与InputStream一起使用。 In that case, call openInputStream() on a ContentResolver to get an InputStream on the content identified by the Uri .在这种情况下,在ContentResolver上调用openInputStream()以获取Uri标识的内容上的InputStream If your "upload to a server" logic only works with files, use that InputStream to copy the content to some temporary file that you control (eg, in getCacheDir() ), then use that temporary file for your upload.如果您的“上传到服务器”逻辑仅适用于文件,请使用该InputStream将内容复制到您控制的某个临时文件(例如,在getCacheDir() ),然后使用该临时文件进行上传。 Delete the temporary file when you are done with it.完成后删除临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM