Android byte数组保存为jpg图片

Android byte数组保存为jpg图片,第1张

1、可以保存RBGA,或者yuv420

2、可以在同一个线程保存,或者抛到异步线程

同一个线程即不需要启动thread,直接调用saveImage

异步线程的话启动thread,用queueEvent把saveImage丢进队列执行

在进行Android开发过程中,我们经常会接触到Drawable对象(官方开发文档:A Drawable is a general abstraction for "something that can be drawn."),那么,若要使用数据库来进行存储及读取.

@Override

public void onCreate(SQLiteDatabase database) {

executeSQLScript(database, "create.sql")

}

private void executeSQLScript(SQLiteDatabase database, string dbname){

ByteArrayOutputStream outputStream = new ByteArrayOutputStream()

byte buf[] = new byte[1024]

int len

AssetManager assetManager = context.getAssets()

InputStream inputStream = null

try{

inputStream = assetManager.open(dbname)

while ((len = inputStream.read(buf)) != -1) {

outputStream.write(buf, 0, len)

}

outputStream.close()

inputStream.close()

String[] createScript = outputStream.toString().split("")

for (int i = 0i <createScript.lengthi++) {

String sqlStatement = createScript[i].trim()

// TODO You may want to parse out comments here

if (sqlStatement.length() >0) {

database.execSQL(sqlStatement + "")

}

}

} catch (IOException e){

// TODO Handle Script Failed to Load

} catch (SQLException e) {

// TODO Handle Script Failed to Execute

}

}

import java.io.File

import android.app.Activity

import android.graphics.Bitmap

import android.graphics.BitmapFactory

import android.os.Bundle

import android.view.View

import android.view.View.OnClickListener

import android.widget.ImageView

public class MainAct extends Activity {

private ImageView img

//图片路径

private String filepath = "/sdcard/sample.jpg"

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.main)

img = (ImageView) findViewById(R.id.img)

File file = new File(filepath)

if (file.exists()) {

Bitmap bm = BitmapFactory.decodeFile(filepath)

//将图片显示到ImageView中

img.setImageBitmap(bm)

}

}

}

请参考


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11591330.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存