将图片发送到android

时间:2015-05-16 16:45:27

标签: java android android-camera

我的应用程序的这一部分遇到了问题,这是我的第一个应用程序,因此我对每个部分使用教程。 在此活动中,我需要允许用户拍摄新照片并将其发送到我的网络服务器。但它在onActivityResult()中有致命错误......

错误就在这一行:

Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);

以下是停止工作的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (true) {
            //requestCode == 100 && resultCode == RESULT_OK

            selectedImage = data.getData();
            photo = (Bitmap) data.getExtras().get("data");

            // Cursor to get image uri to display
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            //ERROR
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
            imageView.setImageBitmap(photo);
        }
    }

这是在它之前运行的代码:

 Button btpic, btnup;
    private Uri fileUri;
    String picturePath;
    Uri selectedImage;
    Bitmap photo;
    String ba1;
    public static String URL = "http://192.168.1.11/CarQuiz/images/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity_camera);


        btpic = (Button) findViewById(R.id.cpic);
        btpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickpic();
            }
        });

        btnup = (Button) findViewById(R.id.up);
        btnup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                upload();
            }
        });
    }

相机的功能:

  private void clickpic() {
        // Check Camera
        if (getApplicationContext().getPackageManager().hasSystemFeature(

                PackageManager.FEATURE_CAMERA)) {
            // Open default camera

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            // start the image capture Intent
            startActivityForResult(intent, 100);

        } else {
            Toast.makeText(getApplication(), "Camera not supported", Toast.LENGTH_LONG).show();
        }
    }

这是logcat中的最后一件事:

    05-16 12:45:57.027    5928-5928/com.XXXXX.YYYYY.carquizXXXX E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.XXXXX.XXXXX.carquizXXXXX, PID: 5928
        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.XXXXXX.YYYYY.carquizXXXXX/com.XXXXX.
YYYYYYYe.carquizCCCCC.Camera}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
                at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
                at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
                at android.app.ActivityThread.access$1300(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
                at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1437)
                at android.content.ContentResolver.query(ContentResolver.java:462)
                at android.content.ContentResolver.query(ContentResolver.java:422)
                at com.helsen.yenthe.carquizhelsen.Camera.onActivityResult(Camera.java:118)
                at android.app.Activity.dispatchActivityResult(Activity.java:6135)
                at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
                at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
                at android.app.ActivityThread.access$1300(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我使用本教程创建了这个:https://trinitytuts.com/capture-image-upload-server-android/

我现在已经坚持了几个小时,非常感谢大家!

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是实现IntentService并通过广播意图通知状态。请查看github的完整代码

Android库,提供易于使用的服务,可通过Android Notification Center集成上传文件

https://github.com/alexbbb/android-upload-service

Android使用进度条上传相机图像,视频到服务器

http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

//用它来获取位图

File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;
                    break;
                }
            }
            Bitmap bm;
            BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
            bm = BitmapFactory.decodeFile(fileUri.getPath(), btmapOptions);

            if (bm != null)
                imageView.setImageBitmap(bm);

如果您在整合此代码时遇到任何问题,请告诉我