在Android中使用Base64编码将图像数据转换为字节码

3

我正在开发一个应用程序,使用SD卡中的图像URI上传图像,将其转换为字节数组并保存在数据库后端。作为一名新的Android开发人员,请给出一些解决方案...

 startActivityForResult(new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), 1);
            }
  public void onActivityResult(int requestCode,int resultCode,Intent data)  
 {
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == Activity.RESULT_OK) 
 {
 Uri selectedImage = data.getData();
 Cursor cur = PhotoImage.this.managedQuery(selectedImage, null, null, null, null);
 if(cur.moveToFirst())
 {
 long Length = cur.getLong(cur.getColumnIndex(ImageColumns.SIZE));
 try{
 String Image=Base64.encodeBytes(selectedImage.getPath().getBytes());
 Log.v("check",Image);
 byte[] bytedata = new byte[(int) Length];
 FileOutputStream fos=new FileOutputStream(Img);
 fos.write(bytedata[0]);
 fos.close();
   }
 catch (Throwable th)
 {}

输出结果为:

12-30 13:00:24.619: VERBOSE/check(773): L2V4dGVybmFsL2ltYWdlcy9tZWRpYS8y

这是我认为未转换为字节数组的显示输出,请提供一些解决方案。


3
有没有“最难以阅读问题”的徽章? - Kevin Gaudin
你在这里干嘛?打开一个流并写入未初始化字节数组的第一个字节? - mtraut
我正在询问如何使用Base64将图像数据转换为字节数组。 - Narasimha
1个回答

2

这段代码将把图像文件转换为字节:

FileInputStream fin = c.openFileInput(path of file);
byte[] imageBytes = new byte[fin.available()];
fin.read(imageBytes);

放在try...catch()

试试这个,告诉我你是在问同样的问题还是其他问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接