1

I have to select a file and send to server. The following code I am using

Gallery Intent to pick a file.

Intent intent = new Intent();
        intent.setType("*/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select Image"), REQUEST_CODE);

OnActivityResult

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

    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {

        if (data != null) {

            Uri selectedImageUri = data.getData();

            // GET IMAGE PATH
            imagePath = getPath(selectedImageUri);
            Log.d("", "Image path : " + imagePath);

            // IMAGE NAME
            imageName = imagePath.substring(imagePath.lastIndexOf("/"));

            imageSize = this.getFileSize(imagePath);

            // DECODE TO BITMAP
            // Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

            // DISPLAY IMAGE
            // imageView.setImageBitmap(bitmap);
            imageLocationTextView.setText("File path :" + imagePath);
        }
    }
}

If i change this line to

intent.setType("image/*");

To this

intent.setType("*/*");

I get error at onActivityResult

 11-30 14:45:24.389: E/AndroidRuntime(3126): java.lang.RuntimeException: Failure delivering result 
 ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/document/0BEE-381E:splash.jpg flg=0x1 }} 
 to activity {shane.atom/shane.atom.MainActivity}: java.lang.NullPointerException:
 Attempt to invoke virtual method 'int java.lang.String.lastIndexOf(java.lang.String)'
 on a null object reference

I upload image scussefully to the server but when i try to upload file to server i failed. Please help suggest any solution.

Thanks in Advance.

Sander de Jong
  • 351
  • 6
  • 18
  • Show `getPath ` method code – ρяσѕρєя K Nov 30 '15 at 09:26
  • Here is ------------ private 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); } –  Nov 30 '15 at 09:28
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Tunaki Nov 30 '15 at 09:31
  • No, its different question. –  Nov 30 '15 at 09:33

2 Answers2

0

Instead of using String. lastIndexOf and substring methods for getting name of image, we can do it using File. getName() method as:

if(imagePath !=null){
 File file=new File(imagePath);
 imageName = file.getName();
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Now i am getting this - 11-30 15:02:03.489: W/System.err(3343): java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference 11-30 15:02:03.489: W/System.err(3343): at java.io.File.fixSlashes(File.java:183) 11-30 15:02:03.489: W/System.err(3343): at java.io.File.(File.java:130) –  Nov 30 '15 at 09:32
  • @S404: at which line getting issue? – ρяσѕρєя K Nov 30 '15 at 09:41
  • I am sharing code - php code - https://www.dropbox.com/s/4u76cqslajpbdkp/uploadtoserver.php?dl=0 java code- https://www.dropbox.com/sh/8afzx4ltlc6gen5/AADa_MIP9qT_SXvuVbuEc6lza?dl=0 please help, i was stuck with from last 8 days. –  Nov 30 '15 at 09:45
  • @S404: share specific issue instead of share full code – ρяσѕρєя K Nov 30 '15 at 09:45
  • Simply i have to upload file (doc, pdf, etc) file to server, I successfully upload the image. But when i try to upload file, i get failed. pls help –  Nov 30 '15 at 09:48
  • @S404: dude, this is another question from which you have posted in your post. instead of making U-TURN in same post. ask separate question for other issue – ρяσѕρєя K Nov 30 '15 at 09:49
-1
public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

Also please check permission of reading and writing external storage in menifest

ripan
  • 78
  • 8