简体   繁体   中英

Getting the path to upload Image from Android to Server

I'm working on code to allow user to choose image from gallery and upload it to server using Base64. Every thing work fine until I test the code on API > 19 and getting an error.

Here is my code:

    // Upload Image :)

    camera.setOnClickListener(new OnClickListener () {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
               Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1);   
        }});


  } // End Of On Create

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {

      if (requestCode == 1 && resultCode == RESULT_OK) {
             //Bitmap photo = (Bitmap) data.getData().getPath(); 

             Uri selectedImageUri = data.getData();
             imagepath = getPath(selectedImageUri);
             Bitmap image =BitmapFactory.decodeFile(imagepath);
             imageView_pic.setImageBitmap(image);
             imageData = encodeTobase64(image);
             Toast.makeText(Share_event_experience.this, imagepath, Toast.LENGTH_LONG).show();
      }
     }

  public static String encodeTobase64(Bitmap image) {
        System.gc();

        if (image == null) return null;

        Bitmap immagex = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        byte[] b = baos.toByteArray();

        String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT); // min minSdkVersion 8

        return imageEncoded;
    }

     public String getPath(Uri uri) {
              String[] projection = { MediaStore.Images.Media.DATA };
              Cursor cursor = managedQuery(uri, projection, null, null, null);
              int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
              cursor.moveToFirst();
              return cursor.getString(column_index);
          }

Then I send the value "imageData" using ASyncTask to php file.

The error I get when I try On API > 19:

04-15 10:16:07.497: E/BitmapFactory(1357): Unable to decode stream: java.lang.NullPointerException

Not an expert, but trying to help. Is it because it cannot find the image at the path?

When you return your result from the activity, can you not add the path as a string.

 Intent myIntenta = new Intent();
 myIntenta.putExtra("path", "\\pathtoimage\filename");
 setResult(9,myIntenta);
 finish();

and then onActivityResult gets it as a string?

imagePath = (String) intent.getStringExtra("path");

Or maybe because you are running the Garbage Collector at an inappropriate time?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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