Android无法在Logcat中打开文件进行阅读

时间:2013-05-11 16:13:12

标签: android eclipse logcat image-upload

我在logcat中发现了这个错误,这是我从网站上获取的代码:

  1. Main.java

    public class Main extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Button upload_btn = (Button) this.findViewById(R.id.upload);
        upload_btn.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // To open up a gallery browser
                  Intent intent = new Intent();
                  intent.setType("image/*");
                  intent.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
    
            }
        });
    
    }
    
    
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
            // currImageURI is the global variable I’m using to hold the content:
                Uri currImageURI = data.getData();
                System.out.println("Current image Path is ----->" + getRealPathFromURI(currImageURI));
                TextView tv_path = (TextView) findViewById(R.id.path);
                tv_path.setText(getRealPathFromURI(currImageURI));
                HttpUploader uploader = new HttpUploader();
                try {
                  String image_name = uploader.execute(getRealPathFromURI(currImageURI)).get();        
                } catch (InterruptedException e) {
                  e.printStackTrace();
                } catch (ExecutionException e) {
                  e.printStackTrace();
                }
    
            }
        }
    
    }
    
    
    
    //Convert the image URI to the direct file system path of the image file
    public String getRealPathFromURI(Uri contentUri) {
        String [] proj={MediaColumns.DATA};
        android.database.Cursor cursor = managedQuery( contentUri,
        proj,     // Which columns to return
        null,     // WHERE clause; which rows to return (all rows)
        null,     // WHERE clause selection arguments (none)
        null);     // Order-by clause (ascending by name)
        int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    

    }

  2. HttpUploader.java

          package com.guerrilla.ptf.view;//set the correct pacage name
    
    import java.io.ByteArrayOutputStream;
    import java.util.ArrayList;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    

    import com.guerrilla.ptf.view.Base64; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log;

    //Uploader class
    public class HttpUploader extends AsyncTask<String, Void, String> {
    
    
        @Override
        protected String doInBackground(String... path) {
            // TODO Auto-generated method stub
             String outPut = null;
    
                for (String sdPath : path) {
    
                    Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    
                    //Resize the image
                    double width = bitmapOrg.getWidth();
                    double height = bitmapOrg.getHeight();
                    double ratio = 400/width;
                    int newheight = (int)(ratio*height);
    
                    System.out.println("———-width" + width);
                    System.out.println("———-height" + height);
    
                    bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
    
                    //Here you can define .PNG as well
                    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
                    byte[] ba = bao.toByteArray();
                    String ba1 = Base64.encodeBytes(ba);
    
                    System.out.println("uploading image now ——–" + ba1);
    
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("image", ba1));
    
    
    
                    try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://10.0.2.2/api.upload.php");
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();                
    
                        // print response
                        outPut = EntityUtils.toString(entity);
                        Log.i("GET RESPONSE—-", outPut);
    
                        //is = entity.getContent();
                        Log.e("log_tag ******", "good connection");
    
                        bitmapOrg.recycle();
    
                    } catch (Exception e) {
                        Log.e("log_tag ******", "Error in http connection " + e.toString());
                    }
                }
                return outPut;
        }
    

    }

  3. 这是logcat错误:

    Applicaton: com.guerrilla.ptf.view
    Text: Can't open file for reading
    

    知道如何解决这个错误吗?

1 个答案:

答案 0 :(得分:-1)

当我尝试在相对布局中加载2个片段时,我看到了类似的错误。我将布局更改为线性,问题解决了!