Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决:java.lang.ClassCastException:android.graphics.Bitmap无法转换为android.net.Uri_Android_Bitmap_Intentservice - Fatal编程技术网

如何解决:java.lang.ClassCastException:android.graphics.Bitmap无法转换为android.net.Uri

如何解决:java.lang.ClassCastException:android.graphics.Bitmap无法转换为android.net.Uri,android,bitmap,intentservice,Android,Bitmap,Intentservice,什么时候我可以在whats应用程序facebook ect上分享图片。其无法读取文件并抛出exeption,原因是: 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

什么时候我可以在whats应用程序facebook ect上分享图片。其无法读取文件并抛出exeption,原因是:

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)
我的活动:

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;
    }

我的图像在布局文件夹中保存到SD卡中,我想在这里共享我的代码plz帮助我

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;
}

}

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

 share.putExtra(Intent.EXTRA_STREAM,
                    scaleDownBitmap(mBitmap, 90, FullImageActivity.this));
像这样改变

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路径

另一种方法是

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());

在您的
share
on click listener中,您在下面写了这一行,这一行是错误:-

share.putExtra(Intent.EXTRA_STREAM,scaleDownBitmap(mBitmap, 90, FullImageActivity.this));
在这种情况下,您将调用
scaleDownBtmap
函数,该函数返回位图。现在inten.putExtra方法不接受位图参数,这就是为什么会出现类强制转换异常。您需要在ByTestStream中转换位图,然后将其传递给put extra方法。 位图缩放向下位图(位图照片、int newHeight、上下文){

}

putExtra方法不接受位图参数。 这样做:-

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());
然后在接收活动中,将此ByTestStream转换为位图:-

Bitmap b = BitmapFactory.decodeByteArray(
    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);

但是我通过它的位置获取图像,所以如何设置Uri,我在任何图像中单击网格视图,它在新活动中打开我想共享此图像,但我不知道写入Uri的内容。为此,你必须将图像保存到某个位置,获取路径,然后获得Uri。添加权限写入外部存储我再次更新我的问题我的图像是什么在drawable文件夹中显示如何获取此代码中的路径您可以获得如下路径..Uri=Uri.parse(“android”)。resource://your.package.here/drawable/image_name");我不会用勺子喂你的。你是写代码还是复制粘贴的?我要编辑我的帖子。再读一遍
Bitmap b = BitmapFactory.decodeByteArray(
    getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);