这是我的代码,它一次选择一个图像并将其转换为base64,但我想从android库中选择多个文件/图像,然后将每个文件/图像转换为base64。我被困在这里。请帮帮我。
以下是我的代码:
public class MainActivity extends AppCompatActivity {
Button selectPDF;
private int REQUESTCODE_PICK_Image=100;
static String fileName;
Uri seletedURI;
int count;
static TextView tv;
static TextView tv1;
static String extension = "";
private static ArrayList<String> galleryImageUrls;
ImageView img1[];
Bitmap yourbitmap, resized;
String imageEncoded;
List<String> imagesEncodedList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.convertedText);
tv1=(TextView)findViewById(R.id.nameOfFile);
selectPDF=(Button)findViewById(R.id.selectPDF);
selectPDF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseFile();
}
});
}
private void chooseFile() {
try {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
try {
startActivityForResult(intent, REQUESTCODE_PICK_Image);
} catch (ActivityNotFoundException e) {
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String convertFileToByteArray() {
byte[] byteArray = null;
File filePath = Environment.getExternalStorageDirectory();
Log.v("New file Path",filePath.toString());
String filename=fileName.substring(fileName.lastIndexOf("/")+1);
Log.v("File Name",filename);
int i = fileName.lastIndexOf('.');
if (i >= 0) {
extension = fileName.substring(i+1);
Log.v("extension",extension);
}
File file = new File(fileName);
//Log.v("Full Path",file.toString());
try {
InputStream inputStream = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024 * 11];
int bytesRead = 0;
while ((bytesRead = inputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
byteArray = bos.toByteArray();
Log.e("Byte array", ">" + byteArray);
} catch (IOException e) {
e.printStackTrace();
}
String convertFile= Base64.encodeToString(byteArray, Base64.NO_WRAP);
tv.setText(convertFile);
Log.v("File Information",convertFile);
return String.valueOf(0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
// When an Image is picked
if (requestCode == REQUESTCODE_PICK_Image && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
String[] filePathColumn = { MediaStore.Images.Media.DATA };
imagesEncodedList = new ArrayList<String>();
if(data.getData()!=null){
seletedURI=data.getData();
if (seletedURI.toString().startsWith("file:")) {
fileName = seletedURI.getPath();
}
} else {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
mArrayUri.add(uri);
// Get the cursor
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
fileName = cursor.getString(columnIndex);
imagesEncodedList.add(fileName);
cursor.close();
}
Log.v("LOG_TAG", "Selected Images " + mArrayUri.size());
}
}
} else {
Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
/*//..............converting files into base64.......*/
convertFileToByteArray();
super.onActivityResult(requestCode, resultCode, data);
}
}