導航:首頁 > 操作系統 > androidbitmap轉uri

androidbitmap轉uri

發布時間:2022-06-16 00:24:47

❶ 如何將從圖庫中查到的圖片轉換成 二進制 android 代碼

1、獲得圖庫返回的URL
2、根據URL獲得圖片的本地絕對地址,構建Bitmap
3、將Bitmap轉換成byte[]數組

public void onActivityResult(int requestCode, int resultCode, Intent data) {

Uri uri = data.getData();

String path=uri.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(path,);
byte[] datas=bitmap2Bytes(bitmap );
}

public byte[] bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}

❷ android怎麼調用系統自帶的圖庫打開指定目錄的相冊

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//完成照相後回調用此方法
super.onActivityResult(requestCode, resultCode, data);
case 1:
switch (resultCode) {
case Activity.RESULT_OK://照相完成點擊確定
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用
Log.v("TestFile", "SD card is not avaiable/writeable right now.");
return; }
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 獲取相機返回的數據,並轉換為Bitmap圖片格式
FileOutputStream b = null;
File file = new File("/sdcard/pk4fun/");
file.mkdirs();// 創建文件夾,名稱為pk4fun // 照片的命名,目標文件夾下,以當前時間數字串為名稱,即可確保每張照片名稱不相同。網上流傳的其他Demo這里的照片名稱都寫死了,則會發生無論拍照多少張,後一張總會把前一張照片覆蓋。細心的同學還可以設置這個字元串,比如加上「IMG」字樣等;然後就會發現sd卡中myimage這個文件夾下,會保存剛剛調用相機拍出來的照片,照片名稱不會重復。
String str = null;
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 獲取當前時間,進一步轉化為字元串
date = new Date(resultCode);
str = format.format(date);
String fileName = "/sdcard/myImage/" + str + ".jpg";
sendBroadcast(fileName);
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數據寫入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
} break;
case Activity.RESULT_CANCELED:// 取消
break;
}
break;
case 2:
switch (resultCode) {
case Activity.RESULT_OK: {
Uri uri = data.getData();
Cursor cursor = mActivity.getContentResolver().query(uri, null,
null, null, null);
cursor.moveToFirst();
String imgNo = cursor.getString(0); // 圖片編號
String imgPath = cursor.getString(1); // 圖片文件路徑
String imgSize = cursor.getString(2); // 圖片大小
String imgName = cursor.getString(3); // 圖片文件名
cursor.close();
// Options options = new BitmapFactory.Options();
// options.inJustDecodeBounds = false;
// options.inSampleSize = 10;
// Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
}
break;
case Activity.RESULT_CANCELED:// 取消
break;
}
break;
}

❸ Android 兩個Activity之間怎樣使用Uri傳遞圖片,怎樣獲取圖片的Uri,怎樣通過Uri得到圖片

圖片類:
class Image implements Serializable{
private String url;
private Bitmap bitmap;
}

傳遞:
Image image = new Image();
image.seturl(url);
image.setbitmap(bitmap);
intent.putExtra("image", image);

獲取
Image image = intent.getSerializableExtra("image");
String url = image.geturl();
Bitmap bitmap =image.getbitmap();

❹ android bitmap對象怎樣轉化成uri

java">

publicstaticUribitmap2uri(Contextc,Bitmapb){
Filepath=newFile(c.getCacheDir()+File.separator+System.currentTimeMillis()+".jpg");
try{
OutputStreamos=newFileOutputStream(path);
b.compress(Bitmap.CompressFormat.JPEG,100,os);
os.close();
returnUri.fromFile(path);
}catch(Exceptionignored){
}
returnnull;
}

❺ android 系統拍照的照片是按照什麼命名的,怎麼能夠將他的uri取出來。急等啊

在多媒體應用製作中,我們經常會用到camera,那麼到底如何調用系統的camera呢:
1.調用相機:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
只需要把這兩句代碼寫進button的onclick事件中即可。
2. 存儲照片:
//使用此方法,以文件形式存儲照片
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

int i = 0;

if (resultCode == Activity.RESULT_OK) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用
Log.v("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 獲取相機返回的數據,並轉換為Bitmap圖片格式
FileOutputStream b = null;
File file = new File("/sdcard/myImage/");
file.mkdirs();// 創建文件夾,名稱為myimage
//照片的命名,目標文件夾下,以當前時間數字串為名稱,即可確保每張照片名稱不相同。網上流傳的其他Demo這里的照片名稱都寫死了,則會發生無論拍照多少張,後一張總會把前一張照片覆蓋。細心的同學還可以設置這個字元串,比如加上「IMG」字樣等;
然後就會發現sd卡中myimage這個文件夾下,會保存剛剛調用相機拍出來的照片,照片名稱不會重復。
String str=null;
Date date=null;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");//獲取當前時間,進一步轉化為字元串
date =new Date();
str=format.format(date);
String fileName = "/sdcard/myImage/"+str+".jpg";
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數據寫入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//注意要加上幾個許可權
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

❻ Android 視頻開發中如何通過url或者本地視

第一步:將bitmap轉換成drawable對象,並設置給surfaceView視頻播放窗口作為背景圖片
//通過getVideoThumbnail方法取得視頻中的第一幀圖片,該圖片是一個bitmap對象Bitmap bitmap=getVideoThumbnail(String url);//將bitmap對象轉換成drawable對象Drawable drawable=new BitmapDrawable(bitmap);//將drawable對象設置給視頻播放窗口surfaceView控制項作為背景圖片surfaceView.setBackgroundDrawable(drawable);123456

第二部分:通過url網址或者本地文件路徑獲得視頻的第一幀圖片
public Bitmap getVideoThumbnail(String url) {
Bitmap bitmap = null;//MediaMetadataRetriever 是android中定義好的一個類,提供了統一//的介面,用於從輸入的媒體文件中取得幀和元數據;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
//()根據文件路徑獲取縮略圖//retriever.setDataSource(filePath);
retriever.setDataSource(url, new HashMap()); //獲得第一幀圖片
bitmap = retriever.getFrameAtTime();
}
catch(IllegalArgumentException e) {
e.printStackTrace();
}
catch (RuntimeException e) {
e.printStackTrace();
}
finally {
try {
retriever.release();
}
catch (RuntimeException e) {
e.printStackTrace();
}
}
Log.v("bitmap", "bitmap="+bitmap); return bitmap;
}

❼ android如何通過path得到uri

最近做項目要通過圖片的絕對路徑找到圖片的URI,然後刪除圖片,小小總結一下獲取URI的方法,親自試驗在

android 4.1.3的系統上都是可用的。

1.將所有的圖片路徑取出,遍歷比較找到需要的路徑,取出URI,效率較低

其中 MediaStore.MediaColumns.DATA 欄位存的就是圖片的絕對路徑,

最後mImageUri得到的就是圖片的URI

1 Uri mUri = Uri.parse("content://media/external/images/media");

2 Uri mImageUri = null;

3 Cursor cursor = managedQuery(

4 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,

5 MediaStore.Images.Media.DEFAULT_SORT_ORDER);

6 cursor.moveToFirst();

7

8 while (!cursor.isAfterLast()) {

9 String data = cursor.getString(cursor

10 .getColumnIndex(MediaStore.MediaColumns.DATA));

11 if (picPath.equals(data)) {

12 int ringtoneID = cursor.getInt(cursor

13 .getColumnIndex(MediaStore.MediaColumns._ID));

14 mImageUri = Uri.withAppendedPath(mUri, "" + ringtoneID);

15 break;

16 }

17 cursor.moveToNext();

18 }2.直接從媒體資料庫根據欄位取出相應的記錄,效率較高

1 //TYLT: add by anyf 20121027 start

2 String type = Utils.ensureNotNull(intent.getType());

3 Log.d(TAG, "uri is " + uri);

4 if (uri.getScheme().equals("file") && (type.contains("image/")))
{

5 String path = uri.getEncodedPath();

6 Log.d(TAG, "path1 is " + path);

7 if (path != null) {

8 path = Uri.decode(path);

9 Log.d(TAG, "path2 is " + path);

10 ContentResolver cr = this.getContentResolver();

11 StringBuffer buff = new StringBuffer();

12 buff.append("(")

13 .append(Images.ImageColumns.DATA)

14 .append("=")

15 .append("'" + path + "'")

16 .append(")");

17 Cursor cur = cr.query(

18 Images.Media.EXTERNAL_CONTENT_URI,

19 new String[] { Images.ImageColumns._ID },

20 buff.toString(), null, null);

21 int index = 0;

22 for (cur.moveToFirst(); !cur.isAfterLast(); cur

23 .moveToNext()) {

24 index = cur.getColumnIndex(Images.ImageColumns._ID);

25 // set _id value

26 index = cur.getInt(index);

27 }

28 if (index == 0) {

29 //do nothing

30 } else {

31 Uri uri_temp = Uri

32 .parse("content://media/external/images/media/"

33 + index);

34 Log.d(TAG, "uri_temp is " + uri_temp);

35 if (uri_temp != null) {

36 uri = uri_temp;

37 }

38 }

39 }

40 }

41 //TYLT: add by anyf 20121027 end3.直接根據路徑通過 ContentProvider 的 delete() 方法刪除圖片,兩行代碼搞定,效率最高

1 String params[] = new String[]{filepath};

2
ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media.DATA + " LIKE ?", params);

❽ Android編程 打開本地文件 文件選擇器

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/b01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

代碼

import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.ContentResolver;
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.ImageView;
public class Lesson_01_Pic extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button)findViewById(R.id.b01);
button.setText("選擇圖片");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 開啟Pictures畫面Type設定為image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT這個Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片後返回本畫面 */
startActivityForResult(intent, 1);
}

});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView imageView = (ImageView) findViewById(R.id.iv01);
/* 將Bitmap設定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}

❾ android 如何獲取保存的圖片的地址 並存到資料庫中

安卓中如何獲取保存的圖片uri 並保存到sqlite資料庫中
有如下兩種方法,僅供參考
方法一:Java代碼

public void saveIcon(Bitmap icon) {
if (icon == null) {
return;
}
// 最終圖標要保存到瀏覽器的內部資料庫中,系統程序均保存為SQLite格式,Browser也不例外,因為圖片是二進制的所以使用位元組數組存儲資料庫的
// BLOB類型
final ByteArrayOutputStream os = new ByteArrayOutputStream();
// 將Bitmap壓縮成PNG編碼,質量為100%存儲
icon.compress(Bitmap.CompressFormat.PNG, 100, os);
// 構造SQLite的Content對象,這里也可以使用
raw ContentValues values = new ContentValues();
// 寫入資料庫的
Browser.BookmarkColumns.TOUCH_ICON欄位 values.put(Browser.BookmarkColumns.TOUCH_ICON, os.toByteArray());
DBUtil.update(....);
//調用更新或者插入到資料庫的方法
}
}

方法二:如果數據表入口時一個content:URIJava代碼

import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;
import java.io.OutputStream;
// Save the name and description of an image in a ContentValues map.
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, "road_trip_1");
values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
values.put(Media.MIME_TYPE, "image/jpeg");
// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
OutputStream outStream = getContentResolver().openOutputStream(uri);
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();
} catch (Exception e) {
Log.e(TAG, "exception while writing image", e);
}
原文請看http://www.bafenbaosoft.com/post/48.html

❿ Android獲取資料庫圖片uri路徑並用imageView顯示

通過流的形式就可以了。通過路徑得到文件流,然後使用bitmapfactory.decodeStream 方法 得到一個bitmap 然偶通過imageView.setImageBitmap()就ok了

閱讀全文

與androidbitmap轉uri相關的資料

熱點內容
androidapp風格 瀏覽:206
php取伺服器url地址 瀏覽:291
linux時間調度演算法 瀏覽:767
單片機最小電路詳解 瀏覽:181
請求要求命令 瀏覽:802
電腦文件夾發微信顯示被佔用 瀏覽:290
手機怎麼看加密視頻 瀏覽:202
怎樣解壓手機es文件包 瀏覽:661
2017年學什麼編程 瀏覽:934
金融期貨pdf 瀏覽:693
程序員客棧的信息保密嗎 瀏覽:507
編程顯示器什麼意思 瀏覽:147
網路編程的就業 瀏覽:260
s7200編程入門 瀏覽:748
華潤萬家app為什麼進不去 瀏覽:250
後台運行app命令 瀏覽:874
通達信雲加密能破解 瀏覽:141
郵件保存成pdf 瀏覽:867
bbs網站java源碼下載百度雲 瀏覽:460
php無限極分類樹 瀏覽:275