如何将Uri图像压缩为位图 [英] how to compress Uri image to bitmap

查看:84
本文介绍了如何将Uri图像压缩为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码显示给我一个错误,我想将所选图像添加到数据库并检索,请按照以下2个教程进行操作 http://androidhub4you.blogspot. com/2012/09/hello-friends-today-i-am-going-to-share.html 但是此问题来自错误消息对于Uri类型,未定义方法compress(Bitmap.CompressFormat,int,ByteArrayOutputStream)"

this code show me an error i want to add selected image save to database and retrive i follow this 2 tutorials http://nizaqatali.wordpress.com/2011/06/21/android-dialog-select-image-from-gallery/ and this http://androidhub4you.blogspot.com/2012/09/hello-friends-today-i-am-going-to-share.html but this problem is come error message"The method compress(Bitmap.CompressFormat, int, ByteArrayOutputStream) is undefined for the type Uri"

         import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
 import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
 import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

 public class SQLiteDemoActivity extends Activity {



final int SELECT_PHOTO = 0;
 ArrayList<Contact> imageArry = new ArrayList<Contact>();
 ContactImageAdapter adapter;
 Button BrowseButton;
 DataBaseHandler db;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

 db = new DataBaseHandler(this);
//get image from drawable
//Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.facebook);


 BrowseButton=(Button)findViewById(R.id.BrowseButton);



  BrowseButton.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
     // in onCreate or any event where your want the user to
    // select a file
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");

    startActivityForResult(photoPickerIntent, SELECT_PHOTO);


 }
  });



 }

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

switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
 Uri selectedImage = imageReturnedIntent.getData();



 //convert bitmap to byte
 ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
/**
* CRUD Operations
* */
 //Inserting Contacts
Log.d("Insert: ", "Inserting ..");
  db.addContact(new Contact("FaceBook", imageInByte));
 //display main List view bcard and contact name

 //Reading all contacts from database
  List<Contact> contacts = db.getAllContacts();
 for (Contact cn : contacts) {
  String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();

//Writing Contacts to log
Log.d("Result: ", log);
 //add contacts data in arrayList
imageArry.add(cn);

}
adapter = new ContactImageAdapter(this, R.layout.screen_list,
 imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
 dataList.setAdapter(adapter);



}
}
}




}

推荐答案

尝试使用此代码希望对您有所帮助

Try to use this code hope it help you

Uri selectedImage = imageReturnedIntent.getData();

InputStream imageStream = null;
try {
    imageStream = getContentResolver().openInputStream(
            selectedImage);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Bitmap bmp = BitmapFactory.decodeStream(imageStream);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
try {
    stream.close();
    stream = null;
} catch (IOException e) {

    e.printStackTrace();
}

这篇关于如何将Uri图像压缩为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆