2017-06-30 54 views
0

我使用parcelable在活動之間傳遞數據(請不要說這是錯誤的方法,因爲有很多關於此的討論)。在意圖中發送Parcelable時顯示白色屏幕當startActivity調用

這是我如何捕獲圖像:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_IMAGE_CAPTURE); 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (intent.resolveActivity(getPackageManager()) != null) { 
      startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); 
     } 
    } 

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     switch (requestCode) { 
      case 30: { 

       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       } else { 

        Toast.makeText(CameraCapture.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); 
       } 
       return; 
      } 
     } 
    } 



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

     dateTimer = getTime(); 

     Log.d("codig",String.valueOf(requestCode)); 

     if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
      Log.d("resultOK","resultOK"); 
      CropImage.activity(data.getData()) 
        .setAspectRatio(1,1) 
        .setInitialCropWindowPaddingRatio(0) 
        .setActivityTitle("Corte a foto") 
        .setActivityMenuIconColor(R.color.nephritis) 
        .start(this); 
     } 
     if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { 
      CropImage.ActivityResult result = CropImage.getActivityResult(data); 
      Log.d("pict12","here"); 
      if (resultCode == RESULT_OK) { 
       Log.d("pict12","here2"); 
       Uri uri = result.getUri(); 
       Bitmap pic = null; 
       try { 
        pic = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       image = encodeImage(pic); 
       showDetailsDialog(data); 
      } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { 
       Exception error = result.getError(); 
       Log.d("pict12",error.toString()); 
      } 
     } 
    } 

當我拍攝照片我開始建設者使用simpleCropView LIB削減圖像,之後返回到第二onactivityResult,和顯示了一個細節對話框是這樣的:

private void showDetailsDialog(final Intent data) { 
     final CharSequence[] items = {"Tem fruto?","É arbusto","É árvore?","Tem flor?","Tem espinhos?"}; 
// arraylist to keep the selected items 
     final ArrayList seletedItems=new ArrayList(); 

     AlertDialog dialog = new AlertDialog.Builder(this) 
       .setTitle("Detalhes da fotografia") 
       .setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) { 
         if (isChecked) { 
          // If the user checked the item, add it to the selected items 
          seletedItems.add(indexSelected); 
         } else if (seletedItems.contains(indexSelected)) { 
          // Else, if the item is already in the array, remove it 
          seletedItems.remove(Integer.valueOf(indexSelected)); 
         } 
        } 
       }).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         hasFruit = seletedItems.contains(0); 
         isBush = seletedItems.contains(1); 
         isTree = seletedItems.contains(2); 
         hasFlower = seletedItems.contains(3); 
         hasThorns = seletedItems.contains(4); 
         if(seletedItems.contains(3)){ 
          dialog.dismiss(); 
          colorDialog(data); 
         } 
         else{ 
          startSimiliarActivity(); 
         } 
        } 

       }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         Log.d("RECREATE","recria"); 
         dialog.dismiss(); 
         CameraCapture.this.recreate(); 
        } 
       }).create(); 
     dialog.show(); 
    } 

如果這裏面細節對話框我檢查一下把我送到另一個對話框來改變花的顏色,如果我不檢查就花送我去similiarActivityMethod WHE我發送用戶到acitvity,我有空白的佈局。

基本上我傳遞一個對象與字節[]填充和其它數據,沒有那麼大,我認爲,當我打電話顯示空白活性我通過parcelable這樣的:

public void startSimiliarActivity(){ 
     Log.d("HELLWOR","HELLOWOR"); 
     Intent intent = new Intent(CameraCapture.this,SimiliarPhotos.class); 
     if(location.getAltitude() != 0.0){ 
      altitude = location.getAltitude() - 50; 
     } 
     Photo photo = new Photo(image,altitude,location.getLongitude(),location.getAltitude(),dateTimer); 

     intent.putExtra("photo",photo); 
     startActivity(intent); 
    } 

和我把它像這樣:

package com.example.afcosta.inesctec.pt.android.models; 

import android.net.Uri; 
import android.os.Parcel; 
import android.os.Parcelable; 

import java.io.Serializable; 
import java.net.URI; 

/** 
* Created by FilipeCosta on 25/05/2017. 
*/ 

public class Photo implements Parcelable { 
    private int id; 
    private Uri image; 
    private byte[] cropedImage; 
    private String path; 
    private Double lat; 
    private Double lon; 
    private Double alt; 
    private String time; 

    public Photo() { 
    } 


    @Override 
    public int describeContents() { 
     return 0; 
    } 

    public Photo(byte[] cropedImage,Double lat, Double lon, Double alt, String time) { 
     this.cropedImage = cropedImage; 
     this.lat = lat; 
     this.lon = lon; 
     this.alt = alt; 
     this.time = time; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeInt(this.id); 
     dest.writeParcelable(this.image, flags); 
     dest.writeByteArray(this.cropedImage); 
     dest.writeString(this.path); 
     dest.writeValue(this.lat); 
     dest.writeValue(this.lon); 
     dest.writeValue(this.alt); 
     dest.writeString(this.time); 
    } 

    protected Photo(Parcel in) { 
     this.id = in.readInt(); 
     this.image = in.readParcelable(Uri.class.getClassLoader()); 
     this.cropedImage = in.createByteArray(); 
     this.path = in.readString(); 
     this.lat = (Double) in.readValue(Double.class.getClassLoader()); 
     this.lon = (Double) in.readValue(Double.class.getClassLoader()); 
     this.alt = (Double) in.readValue(Double.class.getClassLoader()); 
     this.time = in.readString(); 
    } 

    public static final Creator<Photo> CREATOR = new Creator<Photo>() { 
     @Override 
     public Photo createFromParcel(Parcel source) { 
      return new Photo(source); 
     } 

     @Override 
     public Photo[] newArray(int size) { 
      return new Photo[size]; 
     } 
    }; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public Uri getImage() { 
     return image; 
    } 

    public void setImage(Uri image) { 
     this.image = image; 
    } 

    public byte[] getCropedImage() { 
     return cropedImage; 
    } 

    public void setCropedImage(byte[] cropedImage) { 
     this.cropedImage = cropedImage; 
    } 

    public String getPath() { 
     return path; 
    } 

    public void setPath(String path) { 
     this.path = path; 
    } 

    public Double getLat() { 
     return lat; 
    } 

    public void setLat(Double lat) { 
     this.lat = lat; 
    } 

    public Double getLon() { 
     return lon; 
    } 

    public void setLon(Double lon) { 
     this.lon = lon; 
    } 

    public Double getAlt() { 
     return alt; 
    } 

    public void setAlt(Double alt) { 
     this.alt = alt; 
    } 

    public String getTime() { 
     return time; 
    } 

    public void setTime(String time) { 
     this.time = time; 
    } 

我認爲這個問題是因爲當我作了評論我實例化效果很好的照片行至parcelable有關,但我需要的實例:/

對此有何幫助?

+0

請寫出用於獲取Photo類的代碼,以便我們可以提供幫助 – Mahesh

+0

圖像數據通常不適用於可分類。相反,將它寫入(臨時)文件,傳遞路徑並在另一側讀取它。 – RobCo

回答

0

您可以在Intent中發送的數據的大小是有限的(約500Kb)。照片很可能超過了這個限制。

有幾種可能的解決方案,該simpliest可能是在(臨時)寫入數據,而不是FileIntent發送它的(並讀取接收ActivityFile)。

或者只是確保照片不超過限制(例如通過調整大小)。

+0

謝謝大約500kb的問題,我看到很多人談論如何在活動之間傳遞數據,所有的解決方案都有優點和缺點:) – afcosta007

相關問題