Android: Ottenere byte [] da ID immagine
-
12-10-2019 - |
Domanda
Ecco quello che sto facendo:
In onActivityResult, ottengo i dati in un Intent.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
Questo codice ottiene l'ID immagine:
data.getData().getLastPathSegment();
Ora voglio utilizzare questo ID immagine per ottenere byte [] imageData in modo da poter caricare l'immagine su un server.
Come posso fare a questo proposito?
Soluzione
Uri selectedImageUri = data.getData();
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
cursor.moveToFirst();
selectedImagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();
try
{
File myFile = new File(selectedImagePath);
int filelenghth=(int)myFile.length();
byte [] mybytearray = new byte [filelenghth];
BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile));
bis1.read(mybytearray,0,mybytearray.length);
dos.write(mybytearray,0,mybytearray.length); // write the array to the data output stream
// or whatever else you want to do with it
finish();
}
catch(Exception e)
{
e.printStackTrace();
}
Altri suggerimenti
data.getData () tornerà a voi un URI per la vostra immagine. (Controllare i dati! = Null prima!)
allora avete solo bisogno di eseguire un HTTP POST al server.
Si veda il seguente riferimento sul SO per codice postale http: Invio di immagini utilizzando HTTP Post
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow