android - 如何将Uri图像压缩为位图

标签 android bitmap compression bitmapimage

此代码向我显示一个错误我想将选定的图像保存到数据库并检索我遵循这 2 个教程 http://nizaqatali.wordpress.com/2011/06/21/android-dialog-select-image-from-gallery/还有这个http://androidhub4you.blogspot.com/2012/09/hello-friends-today-i-am-going-to-share.html 但是这个问题是错误消息“方法压缩(Bitmap.CompressFormat,int,ByteArrayOutputStream)未定义类型 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);



}
}
}




}

最佳答案

尝试使用这段代码希望它能帮到你

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

关于android - 如何将Uri图像压缩为位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13859769/

相关文章:

android - 协程 : Runblocking freeze Android UI

c# - 如何在图像上使用 Alpha 绘制颜色 - C#/.NET

c++ - 程序最小化时位图消失

assembly - 6502轻量级压缩算法

android - 在 Android 的 ListView CheckBox 中设置 SelectedValue

android - BottomNavigationView 滞后于 fragment 事务

wpf - 从 WPF 应用程序窗口获取位图?

java - 使用 java 的高效 LZ4 多文件压缩

c# - 在 Windows 8 C# 上解压 ZIP 文件

java - Glide v4 中的差异 DiskCacheStrategy