错误捕获Android中不同Android版本的图像

时间:2016-08-03 11:40:49

标签: android bitmap

我必须捕获图像并保存到服务器。我的代码有时会在检查其他移动版本时出错。在棒棒糖中我收到错误 -

  

无法解码流:java.lang.NullPointerException:尝试调用虚方法' char [] java.lang.String.toCharArray()'在...上   null对象引用

在其他手机中收到错误 -

  

android位图工厂无法解码流

 cameraButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    takePhoto();
                }
            });

    public void dispatchTakePictureIntent() {
            Log.e("CREATED PHOTO", "photo pathS = ");
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent
                    .resolveActivity(getApplicationContext().getPackageManager()) != null) {
                // Create the File where the photo should go

                try {
                    photoFile = createImageFile();
                    Log.e("photoFile", "" + photoFile);
                } catch (IOException ex) {
                    // Error occurred while creating the File

                }
                // Continue only if the File was successfully created
                if (photoFile != null) {
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(photoFile));
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            Log.i(TAG, "onActivityResult: " + this);
            try {
            if (requestCode == REQUEST_TAKE_PHOTO
                    && resultCode == Activity.RESULT_OK) {
                try {
                    setPic();
                    new Myfile().execute();

                imagesPath1=mCurrentPhotoPath;





                }catch (Exception e)
                {
                    e.printStackTrace();
                }
            }




        private void setPic() {

            try {
                Log.e("SETPIC",
                        "photo path SETPIC = ");
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inJustDecodeBounds = true;
                Log.i("", "" + mCurrentPhotoPath);
                BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                int photoW = bmOptions.outWidth;
                int photoH = bmOptions.outHeight;



                // Decode the image file into a Bitmap sized to fill the View
                bmOptions.inJustDecodeBounds = false;

                bmOptions.inPurgeable = true;

                Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
                Matrix mtx = new Matrix();
                mtx.postRotate(90);

                Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0,
                        bitmap.getWidth(), bitmap.getHeight(), mtx, true);
                rotatedBMP.recycle();
                if (rotatedBMP != bitmap)
                    bitmap.recycle();



                try {
                    sendPhoto(rotatedBMP);
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }catch(Exception e)
            {
                e.printStackTrace();
            }

        }

    class Myfile extends AsyncTask<Void, Void, Void> {

            // ProgressDialog progress;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog

                mProgressDialog = new ProgressDialog(Events_Edit.this);
                mProgressDialog.setMessage("Loading...");
                mProgressDialog.setIndeterminate(false);
                mProgressDialog.setCancelable(false);
                mProgressDialog.show();

            }

            @Override
            protected Void doInBackground(Void... params) {
                try {
                if (imagesPathList != null) {
                    if (imagesPathList.size() >= 1) {
                        for (int i = 0; i < imagesPath.length; i++) {

                            strImg = imagesPath[i];

                            File f = new File("" + strImg);
                            String path = f.getName();

                            URL_PATH = URL + path;


                            doFileUpload(f);

                        }
                        Log.d("",
                                "no of image are selected" + imagesPathList.size());

                    } else {

                        Log.d("",
                                "no of image are selected" + imagesPathList.size());
                    }
                } else if (!imagesPath1.equalsIgnoreCase("")) {


                        File f = new File("" + imagesPath1);
                            String path = f.getName();

                            URL_PATH = URL + path;


                            Log.e("File", "" + f);
                            doFileUpload(f);

                        //}
                    }
                }catch (Exception e)
                {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                if (mProgressDialog.isShowing())
                {
                    mProgressDialog.dismiss();
                }

                if(URL_PATH!=null){
                    upload_edit.setText("Image Added");
                    upload_edit.setTextColor(Color.GREEN);
                }
            }
        }
    public void doFileUpload(File f) {
            FTPClient client = new FTPClient();

            try {
                client.connect(FTP_HOST, 21);
                Log.e("clientconnect", "" + client);
                client.login(FTP_USER, FTP_PASS);
                Log.e("clientlogin", "" + client);
                client.setType(FTPClient.TYPE_BINARY);
                Log.e("clienttype", "" + client);
                client.changeDirectory("/abc/");

                client.upload(f, new MyTransferListener());

                Log.e("filenameupload", "" + f);
                Log.e("clientupload", "" + client);

            } catch (Exception e) {
                e.printStackTrace();
                try {
                    client.disconnect(true);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }

        }

1 个答案:

答案 0 :(得分:0)

对于所有版本都喜欢这样。

<强>的OnClick:

private void dispatchTakePictureIntent() {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
            }
            if (Constants.photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

OnActivityResult():您现在可以使用路径执行此操作,您可以执行重置。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            if (data != null) {
                String selecteadImage = getRealPathFromURI(mContext, data.getData());
                //startActivity(new Intent(mContext, MainActivity.class).putExtra("path", selecteadImage));
            } else {
                if (Constants.photoFile != null) {
                    String selecteadImage = Constants.photoFile.getAbsolutePath();
                    //startActivity(new Intent(mContext, MainActivity.class).putExtra("path", selecteadImage));
                }
            }
        }
    }

public String getRealPathFromURI(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = {MediaStore.Images.Media.DATA};
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }