从相机拍摄图像并上传到服务器时出现问题

时间:2014-07-29 06:19:53

标签: android camera

我有一个使用相机的应用程序,当我的相机拍摄照片时,我的应用程序崩溃了。然后我想上传图片......

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)         // TODO camera code
    {
        if (resultCode == RESULT_OK)
        {
            String imageId = convertImageUriToFile(imageUri,CameraActivity);
            new LoadImagesFromSDCard().execute(""+imageId);
        }
        else if (resultCode == RESULT_CANCELED)
        {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
        }
        else 
        {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
        }
    }


public class LoadImagesFromSDCard  extends AsyncTask<String, Void, Void> {

        private ProgressDialog Dialog = new ProgressDialog(NewEntry.this);

        Bitmap mBitmap;

        protected void onPreExecute() {
            Dialog.setMessage("Loading image from Sdcard..");
            Dialog.show();
            Toast.makeText(getApplicationContext(), "Loading image from Sdcard", Toast.LENGTH_SHORT).show();
        }

        protected Void doInBackground(String... urls) {

            Bitmap bitmap = null;
            Bitmap newBitmap = null;
            Uri uri = null;       

                try {
                    uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]);
                    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                    Toast.makeText(getApplicationContext(), "try block", Toast.LENGTH_SHORT).show();
                    if (bitmap != null) {
                        newBitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true); 
                        bitmap.recycle();
                        Toast.makeText(getApplicationContext(), "bitmap not null", Toast.LENGTH_SHORT).show();
                        if (newBitmap != null)
                        {
                            mBitmap = newBitmap;
                            Toast.makeText(getApplicationContext(), "newBitmap not null", Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (IOException e) {
                    cancel(true);
                    Toast.makeText(getApplicationContext(), "catch block", Toast.LENGTH_SHORT).show();
                }
            return null;
        }

        protected void onPostExecute(Void unused) 
        {

            Dialog.dismiss();
            if(mBitmap != null)
            {
            }
              //showImg.setImageBitmap(mBitmap);
            Toast.makeText(getApplicationContext(), "mBitmap not null", Toast.LENGTH_SHORT).show();
        }
    }

请帮助我并就此问题向我提出建议:当我拍摄照片时,我的应用程序崩溃了。但之后我想上传图片...... log cat

07-29 11:54:59.173: E/AndroidRuntime(4253): FATAL EXCEPTION: main
07-29 11:54:59.173: E/AndroidRuntime(4253): Process: com.ebiz.eis.realtimedar, PID: 4253
07-29 11:54:59.173: E/AndroidRuntime(4253): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.ebiz.eis.realtimedar/com.ebiz.eis.realtimedar.NewEntry}: java.lang.NullPointerException
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3432)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.app.ActivityThread.access$1300(ActivityThread.java:139)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.os.Looper.loop(Looper.java:136)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.app.ActivityThread.main(ActivityThread.java:5086)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at java.lang.reflect.Method.invokeNative(Native Method)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at java.lang.reflect.Method.invoke(Method.java:515)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at dalvik.system.NativeStart.main(Native Method)
07-29 11:54:59.173: E/AndroidRuntime(4253): Caused by: java.lang.NullPointerException
07-29 11:54:59.173: E/AndroidRuntime(4253):     at com.ebiz.eis.realtimedar.NewEntry.onActivityResult(NewEntry.java:418)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.app.Activity.dispatchActivityResult(Activity.java:5446)
07-29 11:54:59.173: E/AndroidRuntime(4253):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
07-29 11:54:59.173: E/AndroidRuntime(4253):     ... 11 more

1 个答案:

答案 0 :(得分:-1)

请点击此链接BitmapFactory Unable to decode stream

public void decodeFile(String filePath) {
            // TODO Auto-generated method stub
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, o);

            // the new size we want to scale to
            final int REQUIRED_SIZE = 1024;

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                    if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                            break;
                    }
                    width_tmp /= 2;
                    height_tmp /= 2;
                    scale *= 2;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);

            imageview.setImageBitmap(bitmap);
    }