-1

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):

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;
        }
    }
F. Riggio
  • 51
  • 1
  • 2
  • 13
  • Did you tried do internet with startActivityForResult as keyword? First link is telling exactly what you should do. And no, results are not right after you call it. – Selvin Feb 11 '17 at 21:21

1 Answers1

0

You have two problems.

The first one is that startActivityForResult() is not immediate. You do not have any results in the next statement.

So, you are welcome to call startActivityForResult(), as I do in this sample app:

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():

  @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. getPath() only has meaning if the scheme of the Uri is file, and that is rather unlikely. 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.

Ideally, your "upload to a server" logic can work with an InputStream. In that case, call openInputStream() on a ContentResolver to get an InputStream on the content identified by the Uri. 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 (e.g., in getCacheDir()), then use that temporary file for your upload. Delete the temporary file when you are done with it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Of course I am not thinking that I am picking a file. I just want the path to tell the phone later "send this image to the server" – F. Riggio Feb 11 '17 at 21:56
  • Very funny indeed ;-). – greenapps Feb 11 '17 at 21:58
  • @F.Riggio: "Of course I am not thinking that I am picking a file" -- then why are you speaking in terms of paths? Suppose that you have a `Uri` of `http://stackoverflow.com/questions/42181319/get-uri-from-image-in-android/42181636`, and you decide that you want to send the content identified by that `Uri` to a server. Do you think that `/questions/42181319/get-uri-from-image-in-android/42181636` (the result of `getPath()` on that `Uri`) is something that you can use? – CommonsWare Feb 11 '17 at 21:59
  • Because I think it is the best way to tell the phone which is the image it has to send – F. Riggio Feb 11 '17 at 22:01
  • @CommonsWare your code worked but it sent me (with a random image) /document/image:24850. That's not the name of the image, what does it mean? – F. Riggio Feb 11 '17 at 22:06
  • @F.Riggio: You called `getPath()` on a `Uri`. As I wrote in my answer, "`getPath()` only has meaning if the scheme of the `Uri` is `file`, and that is rather unlikely. 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." – CommonsWare Feb 11 '17 at 22:08
  • ah. You are telling me that being an image, what I want to do doesn't have any meaning....I should investigate it more. Thanks! – F. Riggio Feb 11 '17 at 22:13
  • I wanted to do that way also to check if the things that I was doing were okay. How can I check if I took the image that I want to sent successfully? – F. Riggio Feb 11 '17 at 22:14
  • @F.Riggio: "How can I check if I took the image that I want to sent successfully?" -- I am sorry, but I do not know what this means. – CommonsWare Feb 11 '17 at 22:19
  • For example, for my project I had to recieve a JSON object from the server. So, to check if it was okey I made my program shows the JSON object to me in a toast. That was why I wanted to take the path of the image first: to read that what I was doing was okay. How can I check that I took the image that I select at the action_get_content correctly – F. Riggio Feb 11 '17 at 22:25
  • @F.Riggio: Um... show it in an `ImageView`? There are many image-loading libraries that make that process simple, such as Picasso. You hand Picasso the `Uri` and the `ImageView`, and it handles loading the image in the background and displaying it. – CommonsWare Feb 11 '17 at 22:39
  • Don't you think using Uri for that is unnecesary? I was going to show the image from an url directly....It's just set it with the url – F. Riggio Feb 11 '17 at 23:27
  • @F.Riggio: If you mean `setImageURI()`, [that method has issues](https://developer.android.com/reference/android/widget/ImageView.html#setImageURI(android.net.Uri)). For lightweight debugging, if you want to use it, go ahead. For production, use an image-loading library. – CommonsWare Feb 11 '17 at 23:28
  • Ah, ok. It is just for testing right now....So I won't need something so much difficult. Thank you so much for your helping!!! – F. Riggio Feb 11 '17 at 23:31