2013-05-20 205 views
0

我想显示一个图片点击从另一个活动图像视图中的相机,但获取图像的包到位图..我得到这个错误。 我的代码是越来越java.lang.classCastException:android.os.Bundle

package com.example.iwm; 

import java.io.File; 

import java.text.SimpleDateFormat; 


import android.net.Uri; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Environment; 
import android.annotation.SuppressLint; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; 
//private static final int RESULT_OK = -1; 
private static final int MEDIA_TYPE_IMAGE = 1; 

public static Bundle s = null; 
//public static String y ; 
private Uri fileUri; 
private static Intent intent; 
private static Intent intent2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

     // create Intent to take a picture and return control to the calling application 
    intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image 
    intent.putExtra(android.provider.MediaStore.ACTION_IMAGE_CAPTURE, fileUri); // set the image file name 

    // start the image capture Intent 
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 
    //onActivityResult(CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE, int resultCode, intent) 
} 


private Uri getOutputMediaFileUri(int type) { 
    // TODO Auto-generated method stub 
     return Uri.fromFile(getOutputMediaFile(type)); 
} 

/** Create a File for saving an image or video */ 
@SuppressLint("SimpleDateFormat") 
private static File getOutputMediaFile(int type){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "IWMP-Images"); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("IWMP-Images", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
     "IMG_"+ timeStamp + ".jpg"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) 
@Override 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
     if (resultCode == RESULT_OK) { 
      // Image captured and saved to fileUri specified in the Intent 
      Toast.makeText(this, "Image saved to:\n" + fileUri, Toast.LENGTH_LONG).show(); 
      s =data.getExtras(); 

      intent2 = new Intent(MainActivity.this,MainActivity2.class); 

      intent2.putExtra("Image", s); 

      intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      getApplicationContext().startActivity(intent2); 



     } else if (resultCode == RESULT_CANCELED) { 
      // User cancelled the image capture 
     } else { 
      // Image capture failed, advise user 
      Toast.makeText(this, "Image NOT saved ", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

    } 

` 
从这个

我一个一个摞S保存数据(意向),并将其发送到activty2

我活性2码

package com.example.iwm; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.graphics.Bitmap; 

    import android.view.Menu; 
    import android.view.View; 
    import android.widget.ImageView; 
    import android.widget.TextView; 

public class MainActivity2 extends Activity { 
TextView tx; 
Bitmap bmp; 
ImageView ivUserImage; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_activity2); 
    ivUserImage = (ImageView)findViewById(R.id.image); 
    tx = (TextView)findViewById(R.id.text1); 
    //tx.setText(MainActivity.y); 
Bundle bundle = getIntent().getExtras(); 
try 
{ 
bmp =(Bitmap)bundle.get("Image"); 

} 
catch(Exception e) 
{ 
    tx.setText(e.toString()); 
} 
ivUserImage.setImageBitmap(bmp); 
//int i=10; 
} 


public void upload(View view) 
{ 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main_activity2, menu); 
    return true; 
} 

    } 
+0

发表您的堆栈跟踪 – Raghunandan

+0

你要发送包,虽然意图,而不是位图的:'S = data.getExtras();''intent2.putExtra( “图像”,S);使用':' intent2.putExtra(“Image”,s.get(“data”))' – andrew

+0

andrew_pako ..当使用.. intent2.putExtra(“Image”,s.get(“data”))。方法putExtra(String ,布尔型)Intent类型不适用于参数(String,Object)..意味着s.get(“data”)..给出了布尔值。 – yug

回答

1

ü可以尝试这样的

从你的摄像头取得像这样的图像路径

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

final String[] imageColumns = { MediaStore.Images.Media._ID, 
      MediaStore.Images.Media.DATA }; 
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC"; 
    Cursor imageCursor = managedQuery(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, 
      null, null, imageOrderBy); 
    if (imageCursor.moveToFirst()) { 

     String pathoflasttakenimage = imageCursor.getString(imageCursor 
       .getColumnIndex(MediaStore.Images.Media.DATA)); 
    } 
} 

通本ImagePath的捆绑像这样

Intent intent = new Intent(MainActivity.this, MainActivity2.class); 



    Bundle bundle = new Bundle(); 
    bundle.putString("path", pathoflasttakenimage); 

    intent.putExtras(bundle); 

    startActivity(intent); 
在其他活动中使用

这ImagePath的显示图像的ImageView这样

Bundle b = getIntent().getExtras(); 
    String path = b.getString("path"); 

    BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inSampleSize = 2; // for 1/2 the image to be loaded 
     Bitmap thumb = Bitmap.createScaledBitmap(
       BitmapFactory.decodeFile(path, opts), 96, 96, false); 
     ivUserImage.setImageBitmap(thumb); 

让告诉我什么是这样ü面临的问题。

+0

我的包正在存储意图..有一件事我会做IM发布整个代码..检查我在哪里做错了。谢谢, – yug

+0

我也试过了Bitmap bitmap = BitmapFactory.decodeFile(MainActivity.fileUri.toString()); \t \t \t ivUserImage.setImageBitmap(bitmap);但是这个过于安宁的位图= null。我的fileUri有正确的路径 – yug

+0

有你宣布fileUri静态 – Senthil

0

使用bundle.get("data")而不是bundle.get("Image")用于在第二个活动中从Bundle获取位图。尝试,因为:

Bundle bundle = getIntent().getExtras(); 
try 
{ 
     // getting error here. 
    bmp = (Bitmap)bundle.get("data"); //<< use data instead of Image 
    ivUserImage.setImageBitmap(bmp); 
} 
catch(Exception e) 
{ 
    tx.setText(e.toString());} 
} 
+0

until(Bitmap)bundle.get(“data”); ..我在调试器上得到这个Bundle [{Image = Bundle [mParcelledData.dataSize = 76944]}],bt bmp仍然为空。图像视图上没有图像。 ive发布我的整个代码plz检查是否可能..谢谢 – yug

+0

我thnk可能是sumwhere dwn的行我的图像没有得到保存在捆绑中,你得到它保存在SD卡上。:P – yug

+0

我也试过了Bitmap bitmap = BitmapFactory .decodeFile(MainActivity.fileUri.toString()); \t \t \t ivUserImage.setImageBitmap(bitmap);但是这个过于安宁的位图= null。我的fileUri具有正确的路径 – yug