0

我想在我的應用程序中使用相機類。我只想點擊一張圖片並在imageview上設置,然後將其發佈到某個網址上。在網址上發佈工作正常,但有時出現問題,同時點擊任何圖片,並恢復到我從導航到相機應用程序的相同活動。它在HTC wildfire(2.2版本)上工作正常,但有時會給出例外(失敗機會1/25),但是當我在索尼xperia miro或三星選項卡(4.0版本)上測試時,它會多次提供相同的例外(失敗機率20/25) 。我沒有得到問題存在的地方,因爲有時候應用程序運行平穩,沒有任何異常,但4.0或更高版本,它強制關閉多次,但有時可以正常工作。java.lang.RuntimeException:無法恢復活動{packagename.classname}:

的例外是:java.lang.RuntimeException: Unable to resume activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=tabHome, request=1, result=-1, data=Intent { dat=content://media/external/images/media/17271 }} to activity {fable.eventippo/fable.eventippo.EventsIppoPaymentActivity}: java.lang.ClassCastException: fable.eventippo.Home cannot be cast to fable.eventippo.AddEvent

完整代碼在這裏給出。

Button Onclick。

browse = (Button) findViewById(R.id.browse); 
    browse.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      final CharSequence[] items = { "Camera", "Gallery" }; 
      AlertDialog.Builder builder = new AlertDialog.Builder(
        getParent()); 
      builder.setTitle("Browse From"); 
      builder.setItems(items, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int item) { 
        if (items[item] == "Camera") { 
         PackageManager pm = getPackageManager(); 

         if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { 
          Intent i = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

          i.putExtra(MediaStore.EXTRA_OUTPUT, 
            MyFileContentProviders.CONTENT_URI); 

          getParent().startActivityForResult(i, 
            CAMERA_REQUEST); 

         } else { 

          Toast.makeText(getParent(), 
            "Camera is not available", 
            Toast.LENGTH_LONG).show(); 

         } 
        } else if (items[item] == "Gallery") { 
         try { 

          Intent intent = new Intent(); 
          intent.setType("image/*"); 
          intent.setAction(Intent.ACTION_GET_CONTENT); 
          getParent().startActivityForResult(
            Intent.createChooser(intent, 
              "Select Picture"),   PICK_IMAGE); 
         } catch (Exception e) { 
          Toast.makeText(getParent(), e.getMessage(), 
            Toast.LENGTH_LONG).show(); 
          Log.e(e.getClass().getName(), e.getMessage(), e); 
         } 

        } 
       } 

      }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 

     } 
    }); 

在活動結果:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    Bundle bn = data.getExtras(); 
    switch (requestCode) { 
    case PICK_IMAGE: 
     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 
      String filepath = null; 

      try { 
       // OI FILE Manager 
       String filemanagerstring = selectedImageUri.getPath(); 
       // MEDIA GALLERY 
       String selectedImagePath = getPath(selectedImageUri); 
       // logo.setImageURI(selectedImageUri); 
       if (selectedImagePath != null) { 
        filepath = selectedImagePath; 
       } else if (filemanagerstring != null) { 
        filepath = filemanagerstring; 
       } else { 
        Toast.makeText(getApplicationContext(), "Unknown path", 
          Toast.LENGTH_LONG).show(); 
        Log.e("Bitmap", "Unknown path"); 
       } 
       if (filepath != null) { 
        // /upload.setText(filepath); 
        decodeFile(filepath); 
       } else { 
        // filePath = null; 
        bitmap = null; 
       } 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), "Internal error", 
         Toast.LENGTH_LONG).show(); 
       Log.e(e.getClass().getName(), e.getMessage(), e); 
      } 
     } 
     break; 
    case CAMERA_REQUEST: 
     if (resultCode == Activity.RESULT_OK) { 

      File out = new File(getFilesDir(), "newImage.jpg"); 

      if (!out.exists()) { 

       Toast.makeText(getBaseContext(), 

       "Error while capturing image", Toast.LENGTH_LONG) 

       .show(); 

       return; 

      } 

      String filepath = out.getAbsolutePath().toString(); 
      decodeFile(filepath); 

     } 
     break; 
    default: 
    } 

完整的例外是如果你需要什麼更多的,請告訴我下面的圖片Exception is shown here

所示。

由於提前

等待回答

+0

「java.lang.ClassCastException:fable.eventippo.Home無法轉換爲fable.eventippo.AddEvent」看起來很有趣。我們可以看到getParent()的主體嗎?在java文檔中可以使用getparent()的主體( –

+0

)。請看那裏。 –

+0

我做了類似的事情。而且,我在2臺設備上工作良好。但是,我不確定,爲什麼你有時會遇到問題。如果你想我可以粘貼代碼。 – SKK

回答

1

Takepicture按鈕onClickListener:

TakePicture.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyPic-"+ System.currentTimeMillis() + ".jpg"); 
      SelectedImage = Uri.fromFile(file); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, SelectedImage); 
      startActivityForResult(intent,CAMERA_PIC_REQUEST); 

     } 
    }); 

選擇庫按鈕onClickListener:

SelectfromGallery.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent,"Select Picture"), RESULT_LOAD_IMAGE); 

     } 
    }); 

onActivityResult:

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 

     SelectedImage = data.getData(); 
     String filePath = null; 

     try { 
      // IO FILE Manager 
      String filemanagerstring = SelectedImage.getPath(); 

      // MEDIA GALLERY 
      String selectedImagePath = getPath(SelectedImage); 

      if (selectedImagePath != null) { 
       filePath = selectedImagePath; 
      } else if (filemanagerstring != null) { 
       filePath = filemanagerstring; 
      } else { 
       Toast.makeText(getApplicationContext(), "Unknown path", 
         Toast.LENGTH_LONG).show(); 
       Log.e("Bitmap", "Unknown path"); 
      } 

      if (filePath != null) { 
       decodeFile(filePath); 
      } else { 
       bitmap = null; 
      } 
      FROM_GALLERY = true; 

     } catch (Exception e) { 
      Log.e("Uploaderror", e.getLocalizedMessage()); 
     } 
    } 
    else if(requestCode==CAMERA_PIC_REQUEST && resultCode == RESULT_OK){ 

     /*//SelectedImage = data.getData(); 
     Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
     ProfilePic.setImageBitmap(thumbnail);*/ 
     String filePath = null; 
     try { 
      // OI FILE Manager 
      String filemanagerstring = SelectedImage.getPath(); 

      // MEDIA GALLERY 
      String selectedImagePath = getPath(SelectedImage); 

      if (selectedImagePath != null) { 
       filePath = selectedImagePath; 
      } else if (filemanagerstring != null) { 
       filePath = filemanagerstring; 
      } else { 
       Toast.makeText(getApplicationContext(), "Unknown path", 
         Toast.LENGTH_LONG).show(); 
       Log.e("Bitmap", "Unknown path"); 
      } 

      if (filePath != null) { 
       decodeFile(filePath); 
       FROM_GALLERY = false; 
      } else { 
       bitmap = null; 
      } 

     } catch (Exception e) { 
      Log.e("error:", e.getLocalizedMessage()); 
     } 

    } 
    else if (resultCode == Activity.RESULT_CANCELED) 
     { 
     Log.e("STATUS:", "Selecting picture cancelled"); 
     } 
} 

decodeFile方法:

public void decodeFile(String filePath) { 
    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    bitmap = BitmapFactory.decodeFile(filePath, o2); 

    ProfilePic.setImageBitmap(bitmap); 
} 

在這裏你去。我的完整代碼來實現這一目標。我無法進行徹底的測試,因爲我的設備有限。所以,不能肯定地說,這將適用於所有設備。如果你發現問題的解決方案,或者如果這個代碼工作,然後讓我知道。謝謝。快樂的編碼。

相關問題