首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何解决: java.lang.ClassCastException: android.graphics.Bitmap不能转换为android.net.Uri

如何解决: java.lang.ClassCastException: android.graphics.Bitmap不能转换为android.net.Uri
EN

Stack Overflow用户
提问于 2013-11-23 07:27:10
回答 2查看 5.5K关注 0票数 1

当我有时间在什么应用上分享图片的时候,facebook等等。它的文件、读取和抛出失败的原因是:

代码语言:javascript
运行
复制
java.lang.ClassCastException: android.graphics.Bitmap cannot be cast to android.net.Uri
11-23 12:32:02.835: E/AndroidRuntime(11662):         at com.whatsapp.ContactPicker.d(ContactPicker.java:531)
11-23 12:32:02.835: E/AndroidRuntime(11662):         at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:306)
11-23 12:32:02.835: E/AndroidRuntime(11662):         at android.app.Activity.performCreate(Activity.java:4479)
11-23 12:32:02.835: E/AndroidRuntime(11662):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
11-23 12:32:02.835: E/AndroidRuntime(11662):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)

我的活动:

代码语言:javascript
运行
复制
public class FullImageActivity extends Activity {
    ImageView imageView;
    Button buttonsetwallpaper, share;
    String sp_items[] = new String[] { "Setwallpaper", "Share", "Exit" };
    Spinner spinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);
        buttonsetwallpaper = (Button) findViewById(R.id.wallpaper);
        buttonsetwallpaper.getBackground().setColorFilter(0xFF00FF00,
                PorterDuff.Mode.MULTIPLY);
        share = (Button) findViewById(R.id.button2);
        // get intent data
        Intent i = getIntent();
        final int position = i.getExtras().getInt("id");
        final ImageAdapter imageAdapter = new ImageAdapter(this);

        imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.mThumbIds[position]);

        share.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                // Bitmap o = BitmapFactory.decodeFile("/sdcard/." + img_name);
                // Bitmap o =
                // BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/wallpaperapp/");
                Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
                        imageAdapter.mThumbIds[position]);
                // Bitmap mBitmap =
                // BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));
                try {

                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("image/png");
                    System.out.println("*********************" + mBitmap);
                    share.putExtra(
                            Intent.EXTRA_STREAM,
                            scaleDownBitmap(mBitmap, 90, FullImageActivity.this));
                    startActivity(Intent.createChooser(share, "Share Image"));
                } catch (Exception e) {
                    e.printStackTrace();
                    // Toast.makeText(FullImageActivity.this,
                    // "Error setting wallpaper", Toast.LENGTH_SHORT).show();
                }
            }
        });

        buttonsetwallpaper.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                // Bitmap o = BitmapFactory.decodeFile("/sdcard/." + img_name);
                // Bitmap o =
                // BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/wallpaperapp/");
                Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
                        imageAdapter.mThumbIds[position]);
                // Bitmap mBitmap =
                // BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));

                WallpaperManager myWallpaperManager = WallpaperManager
                        .getInstance(getApplicationContext());

                try {
                    myWallpaperManager.setBitmap(mBitmap);
                    Toast.makeText(FullImageActivity.this, "Wallpaper set",
                            Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(FullImageActivity.this,
                            "Error setting wallpaper", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });
    }

    Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) {

        final float densityMultiplier = context.getResources()
                .getDisplayMetrics().density;

        int h = (int) (newHeight * densityMultiplier);
        int w = (int) (h * photo.getWidth() / ((double) photo.getHeight()));

        photo = Bitmap.createScaledBitmap(photo, w, h, true);

        return photo;
    }

我的图片在布局文件夹与保存到sdcard,我想在这里分享,我给我的代码plz帮助我。

代码语言:javascript
运行
复制
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ImageView imageView;
// Keep all Images in array
public Integer[] mThumbIds = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
        R.drawable.a10, R.drawable.a11, R.drawable.a12, R.drawable.a13,
        R.drawable.a14, R.drawable.a15, R.drawable.a16, R.drawable.a17,
        R.drawable.a18, R.drawable.a19, R.drawable.a20, R.drawable.a21,

        R.drawable.a22, R.drawable.a23, R.drawable.a24, R.drawable.a25,
        R.drawable.a26, R.drawable.a27, R.drawable.a28, R.drawable.a30,
        R.drawable.a31, R.drawable.a32, R.drawable.a33, R.drawable.a34,
        R.drawable.a35, R.drawable.a36, R.drawable.a37,

        R.drawable.a38, R.drawable.a39, R.drawable.a4, R.drawable.a40,
        R.drawable.a41, R.drawable.a42, R.drawable.a43, R.drawable.a5,
        R.drawable.a6, R.drawable.a7, R.drawable.a8, R.drawable.a9,
        R.drawable.aa9

};

// Constructor
public ImageAdapter(Context c) {
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);

    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(250, 300));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(4, 4, 4, 4);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);

    return imageView;
}

}

EN

回答 2

Stack Overflow用户

发布于 2013-11-23 07:32:32

在这一行中,您必须传递保存的映像uri,而不是Bitamp

代码语言:javascript
运行
复制
 share.putExtra(Intent.EXTRA_STREAM,
                    scaleDownBitmap(mBitmap, 90, FullImageActivity.this));

像这样改变..。

代码语言:javascript
运行
复制
Bitmap icon=scaleDownBitmap(mBitmap, 90, FullImageActivity.this);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
} catch (IOException e) {                       
        e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg")

screenshotUri是图像的Uri路径。

另一种方式是..。

代码语言:javascript
运行
复制
Bitmap icon=scaleDownBitmap(mBitmap, 90, FullImageActivity.this);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
share.putExtra(Intent.EXTRA_STREAM, bytes.toByteArray());
票数 2
EN

Stack Overflow用户

发布于 2013-11-23 07:40:10

在单击侦听器的share中,您在下面写了这一行,这一行是错误:-

代码语言:javascript
运行
复制
share.putExtra(Intent.EXTRA_STREAM,scaleDownBitmap(mBitmap, 90, FullImageActivity.this));

在这种情况下,您将调用返回位图的scaleDownBtmap函数。现在,inten.putExtra方法不使用位图参数,这就是为什么要获得类强制转换异常。您需要在字节流中转换位图,然后在put额外方法中传递它。位图scaleDownBitmap(位图照片,int newHeight,上下文上下文){

代码语言:javascript
运行
复制
final float densityMultiplier = context.getResources()
        .getDisplayMetrics().density;

int h = (int) (newHeight * densityMultiplier);
int w = (int) (h * photo.getWidth() / ((double) photo.getHeight()));

photo = Bitmap.createScaledBitmap(photo, w, h, true);

return photo;

}

putExtra方法不使用位图参数。这样做:-

代码语言:javascript
运行
复制
Bitmap b = scaleDownBitmap(mBitmap, 90, FullImageActivity.this); // your bitmap // use your scaleDownBitmap ere
ByteArrayOutputStream bs = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 50, bs);
share.putExtra("byteArray", bs.toByteArray());

然后在接收活动中,将这个字节流转换为一个位图:-

代码语言:javascript
运行
复制
Bitmap b = BitmapFactory.decodeByteArray(
    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20159785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档