2017-01-18 2 views
-2

Die onActivityResult() nicht genannt habe, wenn ich Bild von der Kamera oder eine Galerie und meine Tätigkeit abgestürzt wählen, mein Code ist unten:onActivityResult() stellt nicht

private void selectImage() { 

final CharSequence[] options = { "cam", "gallery","cancel" }; 

AlertDialog.Builder builder = new AlertDialog.Builder(FragmentActivity4.this); 
builder.setTitle("add"); 
builder.setItems(options, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int item) { 

     boolean result=Utility.checkPermission(FragmentActivity4.this); 

     if (options[item].equals("cam")) 
     { 
      if(result){ 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg"); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
          startActivityForResult(intent, 1); 
     }} 
     else if (options[item].equals("gallery")) 
     { 
      final Intent galleryIntent = new Intent(); 
      galleryIntent.setType("image/*"); 
      galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 
      final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source"); 

      startActivityForResult(chooserIntent, 2); 
     } 
     else if (options[item].equals("cancel")) { 
      ivImage.setImageDrawable(ContextCompat.getDrawable(FragmentActivity4.this, R.drawable.plusrred)); 


      dialog.dismiss(); 
     } 
    } 
}); 
builder.show(); 
} 

und in meinem onActivityResult():

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

super.onActivityResult(requestCode, resultCode, data); 




if (resultCode ==Activity.RESULT_OK) { 

    if (requestCode == 1) { 
     File f = new File(Environment.getExternalStorageDirectory().toString()); 
     for (File temp : f.listFiles()) { 
      if (temp.getName().equals("temp.jpg")) { 
       f = temp; 
       break; 
      } 
     } 
     try { 
      Bitmap bitmap; 
      BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

      bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
        bitmapOptions); 

      ivImage.setImageBitmap(bitmap); 
      Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.03), (int)(bitmap.getHeight()*0.03), true); 
      ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); 
      bitmap1.compress(Bitmap.CompressFormat.PNG, 0, baos1); 
      byte[] imgBytes = baos1.toByteArray(); 
      base64String1 = Base64.encodeToString(imgBytes, Base64.DEFAULT); 
      String path = android.os.Environment.getExternalStorageDirectory() 
        + File.separator 
        + "Phoenix" + File.separator + "default"; 
      f.delete(); 
      OutputStream outFile = null; 
      File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg"); 
      try { 
       outFile = new FileOutputStream(file); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 0, outFile); 
       outFile.flush(); 
       outFile.close(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } else if (requestCode == 2) { 

     Uri selectedImage = data.getData(); 
     String[] filePath = { MediaStore.Images.Media.DATA }; 
     Cursor c = FragmentActivity4.this.getContentResolver().query(selectedImage,filePath, null, null, null); 
     c.moveToFirst(); 
     int columnIndex = c.getColumnIndex(filePath[0]); 
     String picturePath = c.getString(columnIndex); 
     c.close(); 
     Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath)); 
     Log.w("path of image from gallery......******************.........", picturePath+""); 
     ivImage.setImageBitmap(thumbnail); 


     Bitmap thumbnail1 = Bitmap.createScaledBitmap(thumbnail,(int)(thumbnail.getWidth()*0.03), (int)(thumbnail.getHeight()*0.03), true); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     thumbnail1.compress(Bitmap.CompressFormat.PNG,0, baos); 
     byte[] imgBytes = baos.toByteArray(); 
base64String1 = Base64.encodeToString(imgBytes, 
       Base64.DEFAULT); 


    } 
} 

Meine Aktivität erweitert AppCompatActivity.

Können Sie mir bitte sagen, wo das Problem liegt?

+0

Betrachten Sie eine Antwort zu akzeptieren, wenn geholfen. – W4R10CK

Antwort

0

Hier haben Sie startActivityForResult(chooserIntent, 2);

und in Ihrer onActivityResult Methode, die Sie enthalten keine 2 in Ihrer if-Anweisung. Ich denke, das Problem besteht darin.

0

Ihre RequestCode ist 2.

ändern diese:

if (requestCode == 1) { 

mit diesem:

if (requestCode == 2) { 
Verwandte Themen