1

FilePath =/external/images/media/2

Getting error : java.io.FileNotFoundException: /external/images/media/2 (No such file or directory)

Hi I am tring to upload the selected image on server .I am getting this error java.io.FileNotFoundException: /external/images/media/2 (No such file or directory).Actually I am checking this on simulator.First I select the image from gallery then get the bytes of selected image.but I am getting error file not found why

here is my code :

package com.CA.xmlparsing;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {

    private static final int PICKFILE_RESULT_CODE = 2;
    private String FilePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void upLoadImage(View view) {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                PICKFILE_RESULT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.", 
                Toast.LENGTH_SHORT).show();
    }

    }  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Fix no activity available
        if (data == null)
            return;
        switch (requestCode) {
        case PICKFILE_RESULT_CODE:
            if (resultCode == RESULT_OK) {
                 FilePath = data.getData().getPath();
                //FilePath is your file as a string
                Log.d("--------------", FilePath);
// print /external/images/media/2
                byte[] bytes=  getFileByte(FilePath);
                Log.d("--------------", ""+bytes);
            }
        }
    }

  private   byte [] getFileByte(String path){
         File file = new File(path);
            int size = (int) file.length();
            byte[] bytes = new byte[size];
            try {
// getting error on this line
                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
                buf.read(bytes, 0, bytes.length);
                buf.close();
            } catch (FileNotFoundException e) {
//catch here the error
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return bytes;
    }
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.CA.xmlparsing.MainActivity" >

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="uploadImage"
     android:onClick="upLoadImage" />

</RelativeLayout>

is there any permission required for thar to read image from emulator ?

user944513
  • 12,247
  • 49
  • 168
  • 318

2 Answers2

1

What you get is not a file path for the file system but a media path in use by mediastore and others. In order to get a real filepath you have to invoke a cursor on the media store. You can find many example codes on this site.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

There are permissions that you need to add to your android manifest although this may not be your only problem. Add:

<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...

AmaJayJB
  • 1,453
  • 2
  • 14
  • 21