2016-06-05 3 views
-1

Ich versuche, das Kunden-Thumbnail zu seinem Konto hinzufügen, indem Sie das Foto erfassen und das Gesicht durch eine andere Aktivität zuschneiden.Es funktioniert gut im Emulator, aber wenn auf dem tatsächlichen Gerät ausgeführt wurde error.Help benötigt.Image Capturing Android Problem

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; 
     } 
    } 

Cropping Aktivität:

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; 
    } 
} 

Fehler:

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) 

Antwort

0

Das Problem mit CropImage.java Linie zu sein scheint 43, aber ich nicht die Quelle, was das Problem zu sagen, sehen könnte anders als der Stack-Trace sagen, es heißt. Length() auf einem Null-Array

+0

für (Dateitemp: f.listFiles()), das ist Zeile 43 –

+0

Überprüfen Sie, ob f tatsächlich korrekt ausgefüllt wird. Haben Sie die write_external_storage-Berechtigung in Ihrem Android-Manifest deklariert? Wenn Sie die Targeting-API 23 oder höher verwenden, müssen Sie die gleiche Berechtigung vom Benutzer anfordern. – seanAshmore

+0

Ich denke, dass Sie wirklich die Lese-Speicherberechtigung benötigen, aber schreiben Sie lesen und schreiben – seanAshmore

0

Sie konvertieren gerade Ich stricke in dieser Zeile. Entferne die Methode .toString() und verwende stattdessen einen Pfad deines Bildes wie Datei f = neue Datei (Environment.getExternalStorageDirectory() + "/ ihr_Verzeichnisname");

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); 
} 
[email protected] 
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); 
}