2014-11-04 50 views
-1

我有下面的代碼:Android的意圖數據爲空

Uri imageUri = null; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_frontpage); 

this.imageView = (ImageView) this.findViewById(R.id.pictureImageView); 
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

Bitmap photo = (Bitmap) data.getExtras().get("data"); 
imageView.setImageBitmap(photo); 

Intent frontPageIntent = new Intent(this, FrontPageActivity.class); 
imageUri = data.getData(); 

frontPageIntent.putExtra(URI_PATH, imageUri.toString()); 
frontPageIntent.putExtra("MapPhoto", photo); 

startActivity(frontPageIntent); 
} 
} 
} 

它告訴我,我的data.getData();一片空白。我試過記錄它,我得到空。代碼工作之前,但現在不知何故我的程序崩潰,因爲這個值爲空,而不是圖像的uri字符串了。

任何人都可以發現我的問題嗎?

回答

0

試試這個:

data.getextras().get("put_here_what _you want to get"); 
+0

不完全確定該怎麼做? – 2014-11-04 09:45:58

0

檢查這個代碼,它會幫助你:

private Bitmap rotatedBitmap = null; 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
try { 
    intent.putExtra("TAG_ORIENTATION", "portrait"); 
    intent.putExtra("return-data", true); 
    startActivityForResult(intent, 0); 
} catch (ActivityNotFoundException e) { 
} 

// OnActivity結果

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     //super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      Uri selectedImageUri = null; 
      if (data != null) { 

        thumbnail = (Bitmap) data.getExtras().get("data"); 
        //don't use Exif rotation here. 
        rotatedBitmap = Bitmap.createScaledBitmap(thumbnail, 150, 150, true); 
        /** Convert bitmap into byte */ 
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100,stream); 
        //byte[] byteArray = stream.toByteArray(); 
        mimEditProfileImage.setImageBitmap(rotatedBitmap); 

      }  
      Toast.makeText(this, "Images Selected",Toast.LENGTH_LONG).show(); 
     } 
    } 


String urlString = ""; 
try { 
    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(urlString); 
    //FileBody bin = null; 
    MultipartEntity reqEntity = new MultipartEntity(); 

    /*if(driverImage!=null){ 
     bin = new FileBody(driverImage); 
     reqEntity.addPart("Image", bin); 
    }*/ 

    if(rotatedBitmap != null){ 
     Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 

     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     rotatedBitmap.compress(CompressFormat.JPEG, 75, bos); 
     byte[] data = bos.toByteArray(); 
     ByteArrayBody bab = new ByteArrayBody(data,"dcc/"+n+".jpg"); 

     reqEntity.addPart("Image", bab);    
    } 
post.setEntity(reqEntity); 
      HttpResponse response = client.execute(post); 
      HttpEntity resEntity = response.getEntity(); 
      response_str = EntityUtils.toString(resEntity); 
      if (resEntity != null) { 
       //do some work 
      } 
     } catch (Exception ex) { 
      Log.e("Debug", "error: " + ex.getMessage(), ex); 
     } 
+0

事情是,我希望我的Uri去我的下一個意圖,所以我可以稍後將它發送到Web服務。但爲此,我需要圖像的Uri,這樣我才能掌握它。你的代碼只是將Uri設置爲null,之後就不會觸及它,所以對我來說並沒有多大的幫助。 – 2014-11-04 09:51:05

+0

好的..這意味着你想發佈這個圖像到服務器也:) – samsad 2014-11-04 09:52:39

+0

代碼編輯cheked一次:) – samsad 2014-11-04 09:56:13

0

只是要你的onCreate這種變化()

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
try { 

intent.putExtra("return-data", true); 

startActivityForResult(cameraIntent, CAMERA_REQUEST); 

} catch (ActivityNotFoundException e) { 

e.printStackTrace(); 

} 
+0

明天我會試試這個第一件事,謝謝! – 2014-11-04 12:14:50