图像捕获Android问题

时间:2016-06-05 09:10:05

标签: android android-capture

我正在尝试通过捕获照片并通过另一个活动裁剪脸部来将客户缩略图添加到他的帐户。它在模拟器中工作正常,但在实际设备上运行时出现错误。需要帮助。

public void imageload(View v)
    {
        switch (v.getId()) {


            case imageView_new_customer:
                final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Add Photo!");
                builder.setItems(options, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (options[item].equals("Take Photo")) {
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
                            File directory = contextWrapper.getDir("Image", Context.MODE_PRIVATE);
                            File f = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                            startActivityForResult(intent, 1);
                        } else if (options[item].equals("Choose from Gallery")) {
                            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(intent, 2);

                        } else if (options[item].equals("Cancel")) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.show();
                break;
        }
    }

裁剪活动:

if (getIntent().getStringExtra("Source").equals("camera"))
{
    // Set image for cropping
    ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir("Image", Context.MODE_PRIVATE);
File f = new File(Environment.getExternalStorageDirectory().toString());


for (File temp : f.listFiles()) {
    if (temp.getName().equals("temp.jpg")) {
        f = temp;
        break;
    }
}

错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{loginscreen.example.com.girviapp/loginscreen.example.com.girviapp.CropImage}: java.lang.NullPointerException: Attempt to get length of null array
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2555)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
            at android.app.ActivityThread.access$800(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5706)
            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:1033)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
     Caused by: java.lang.NullPointerException: Attempt to get length of null array
            at loginscreen.example.com.girviapp.CropImage.onCreate(CropImage.java:43)
            at android.app.Activity.performCreate(Activity.java:6092)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2502)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
            at android.app.ActivityThread.access$800(ActivityThread.java:182)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5706)
            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:1033)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)

3 个答案:

答案 0 :(得分:0)

这个问题似乎与CropImage.java第43行有关,但我看不出来源说明问题可能是什么,而不是堆栈跟踪说它在空数组上调用.length()

答案 1 :(得分:0)

您正在将thr uri转换为该行中的字符串remove .toString()方法而是使用图像的路径 文件f =新文件(Environment.getExternalStorageDirectory()+“/ your_directory_name”);

答案 2 :(得分:0)

1.<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

2.public int REQUEST_CAMERA = 0, SELECT_FILE = 1;    
 final CharSequence[] items = {"Take Photo", "Choose from Library",
            "Cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                userChoosenTask = "Take Photo";
                if (result)
                    cameraIntent();
            } else if (items[item].equals("Choose from Library")) {
                userChoosenTask = "Choose from Library";
                if (result)
                    galleryIntent();
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
3. private void cameraIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        String fileName = "temp.jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        uri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                values);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(takePictureIntent, REQUEST_CAMERA);
    }
}
4.private void galleryIntent() {
    Intent intentPickImage = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intentPickImage, SELECT_FILE);
}
5.@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE) {
            Uri uri = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            onSelectFromGalleryResult(data);
        } else if (requestCode == REQUEST_CAMERA) {
            onCaptureImageResult(data);
        }  else {
            Toast.makeText(getActivity(), "You haven't picked Image", Toast.LENGTH_SHORT).show();
        }
    }
}
6.private void onSelectFromGalleryResult(Intent data) {
    if (data != null) {
        try {
            Uri uri = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getActivity().getContentResolver().query(uri,
                    filePathColumn, null, null, null);

            if (cursor == null || cursor.getCount() < 1) {
                return; // no cursor or no record. DO YOUR ERROR HANDLING
            }
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            PicturePath = cursor.getString(columnIndex);
            if (columnIndex < 0) // no column index
                return; // DO YOUR ERROR HANDLING
            String image = getStringImage(bitmap);
            PicturePath = cursor.getString(columnIndex);
            String filename=PicturePath.substring(PicturePath.lastIndexOf("/")+1);
            txtFilePath.setText(filename);
            cursor.close(); // close cursor
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(getActivity(), "No Data Found", Toast.LENGTH_SHORT).show();
    }
}
7. public void onCaptureImageResult(Intent data) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().managedQuery(uri, projection, null,
            null, null);
    int column_index_data = cursor.getColumnIndexOrThrow(
            MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    PicturePath = cursor.getString(column_index_data);
    String filename=PicturePath.substring(PicturePath.lastIndexOf("/")+1);
    txtFilePath.setText(filename);
}