tungdx / android-media-picker

A library for choose photos, videos from Android devices.
185 stars 57 forks source link

java.io.FileNotFoundException #5

Open Noshad22 opened 8 years ago

Noshad22 commented 8 years ago

i had an issue with the following code lines i dont know how can i solve this i tried alot but i am fail java.io.FileNotFoundException mediaItem.getUriCropped() my code is so simple i put mediaitem.getUriCropped into myimage_path = new File(mediaItem.getUriCropped().toString()); myimage_path.getAbsolutePath(); and after that i pass it to multipart calss where it can be send to server after doing this i am recieving this in my logcat...............

01-19 12:29:41.929 31583-31609/app.bir.com.truelie E/exception﹕ sendPostRequest java.io.FileNotFoundException: /file:/storage/emulated/0/Android/data/app.bir.com.truelie/files/caches/1453188569226217639967.tmp: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:409) at java.io.FileInputStream.(FileInputStream.java:78) at app.bir.com.truelie.post.MultipartPost.postFileParameter(MultipartPost.java:90) at app.bir.com.truelie.post.MultipartPost.send(MultipartPost.java:49) at app.bir.com.truelie.Addnew.addtruelie$Post_Truelie.doInBackground(addtruelie.java:491) at app.bir.com.truelie.Addnew.addtruelie$Post_Truelie.doInBackground(addtruelie.java:457) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:841) Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) at libcore.io.IoBridge.open(IoBridge.java:393)             at java.io.FileInputStream.(FileInputStream.java:78)             at app.bir.com.truelie.post.MultipartPost.postFileParameter(MultipartPost.java:90)             at app.bir.com.truelie.post.MultipartPost.send(MultipartPost.java:49)             at app.bir.com.truelie.Addnew.addtruelie$Post_Truelie.doInBackground(addtruelie.java:491)             at app.bir.com.truelie.Addnew.addtruelie$Post_Truelie.doInBackground(addtruelie.java:457)             at android.os.AsyncTask$2.call(AsyncTask.java:287)             at java.util.concurrent.FutureTask.run(FutureTask.java:234)             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)             at java.lang.Thread.run(Thread.java:841)................

and the other thing i am using universalImageloader library to set image on my imageView i use the following line of code and its working fine ...................... imageLoader.getInstance().displayImage(mediaItem.getUriCropped().toString(), mCropImageView);

please also have a look at my multipart class ..........

public class MultipartPost {

private final String TAG = "MultipartPost";
private List<PostParameter> params;
private static final String CRLF = "\r\n";
private static final String BOUNDARY = "AaB03x";

public MultipartPost(List<PostParameter> params) {
    this.params = params;
}

public String send(String urlString) throws Exception {

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String response = null;
    InputStream is = null;

    try {
        conn = (HttpURLConnection) new URL(urlString).openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
        dos = new DataOutputStream(conn.getOutputStream());

        for(PostParameter param : params) {
            Log.d(TAG, "Processning param: " + param.getParamName());
            if(param.getValue() == null) {
                param.setValue("");
            }
            if(param.getValue().getClass() == File.class) {
                postFileParameter(dos, param.getParamName(), (File) param.getValue(), param.getContentType());
            } 
            else {
                postStringParameter(dos, param.getParamName(), param.getValue().toString());
            }
        }

        dos.writeBytes(closeBoundary());
        dos.flush();

        is = conn.getInputStream();
        int ch;

        StringBuffer b = new StringBuffer();
        while ((ch = is.read()) != -1) {
            b.append((char) ch);
        }
        response = b.toString();
        Log.i("MultiPart Res", response);
    } 
    finally {
        if(dos != null) try { dos.close(); } catch(IOException ioe) { /* that's it */ }
        if(is  != null) try { is .close(); } catch(IOException ioe) { /* that's it */ }
    }

    return response;
}

private void postStringParameter(DataOutputStream dos, String paramName, String paramValue) throws IOException {
    dos.writeBytes(boundary() + CRLF);
    dos.writeBytes("Content-Disposition: form-data; name=\"" + paramName + "\"" + CRLF + CRLF);
    dos.writeBytes(paramValue + CRLF);
}

private void postFileParameter(DataOutputStream dos, String paramName, File file, String contentType) throws IOException {
    dos.writeBytes(boundary() + CRLF);
    dos.writeBytes("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + file.getName() + "\"" + CRLF);
    dos.writeBytes("Content-Type: "+ contentType + CRLF);
    dos.writeBytes("Content-Transfer-Encoding: binary" + CRLF);
    dos.writeBytes(CRLF);

    FileInputStream fileInputStream = new FileInputStream(file);
    int bytesAvailable = fileInputStream.available();
    int maxBufferSize = 1024;
    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte[] buffer = new byte[bufferSize];

    int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0) {
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = null;
        buffer = new byte[bufferSize];
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }
    dos.writeBytes(CRLF);
    dos.flush();
    fileInputStream.close();
    System.gc();
}

private String closeBoundary() {
    return boundary() + "--" + CRLF;
}

private String boundary() {
    return "--" + BOUNDARY;
}

}

lizaifang commented 8 years ago

@Noshad22 maybe use mediaitem..getPathCropped(mContext)