Carica foto da Android App a Google Cloud Storage / APP Motore - Carattere illegale '_'
-
21-12-2019 - |
Domanda
Ho problemi a caricare le foto dalla mia app Android a GCS.Sono in grado di caricare file di testo ma non foto.Ho provato vari tipi di mime oltre a diversi metodi di codifica di base64 (DecodeBase64, encodeBase64urlsafestring, ecc ...)
Mi sento come se fossi davvero vicino.
Questo è il messaggio di errore che ricevo:
.com.google.appicina.repacked.org.codehaus.jackson.jsonparseexception: Carattere illegale '_' (codice 0x5F) nel contenuto di base64 a [Fonte: N / A; Linea: -1, colonna: -1]
Ho guardato la stringa codificata e non ci sono "_" lì dentro.
Codice Android:
Activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case 0:
{
try
{
InputStream is = getContentResolver().openInputStream(data.getData());
tvMessage.setText("Done!");
byte[] b = getBytes(is);
gaeTask task = new gaeTask();
PhotoObject p = new PhotoObject();
p.encodeBytes(b);
p.setName("picturejpg.jpg");
task.execute(p);
}
}
AsnycTask:
protected String doInBackground(PhotoObject... params)
{
String responseMessage = "";
try
{
PhotoObjectEndpoint builder = new PhotoObjectEndpoint(AndroidHttp.newCompatibleTransport(), new JacksonFactory(), new HttpRequestInitializer()
{
@Override
public void initialize(HttpRequest arg0) throws IOException
{
// TODO Auto-generated method stub
}
});
PhotoObject p = params[0];
builder.insertPhotoObject(p).execute();
responseMessage = p.getName() + " was successfully deployed.";
}
.
Codice GCS / GCS:
GAE – PhotoObject:
@Entity
public class PhotoObject
{
public PhotoObject(){}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key key;
public Key getKey()
{
return key;
}
public void setKey(Key key)
{
this.key = key;
}
private String mBytes;
public byte[] getBytes()
{
return decodeBytes();
}
public void setBytes(byte[] mBytes)
{
this.mBytes = encodeBytes(mBytes);
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
private FileType type;
public FileType getType()
{
return type;
}
public void setType(FileType type)
{
this.type = type;
}
/**
*
* @see #getBytes()
* @return Base64 decoded value or {@code null} for none
*
* @since 1.14
*/
public byte[] decodeBytes()
{
return com.google.api.client.util.Base64.decodeBase64(mBytes);
}
/**
*
* @see #setBytes()
*
* <p>
* The value is encoded Base64 or {@code null} for none.
* </p>
*
* @since 1.14
*/
public String encodeBytes(byte[] bytes)
{
//this.mBytes = com.google.api.client.util.Base64.encodeBase64URLSafeString(bytes);
this.mBytes = com.google.api.client.util.Base64.encodeBase64String(bytes);
return this.mBytes;
}
}
GAE - insertPhotoObject:
try
{
final GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
GcsFilename name = new GcsFilename("testbucket123", fileName);
GcsFileOptions.Builder optionsBuilder = new GcsFileOptions.Builder();
optionsBuilder.mimeType("image/jpg");
GcsOutputChannel outputChannel = gcsService.createOrReplace(name, optionsBuilder.build());
ObjectOutputStream out = new ObjectOutputStream(Channels.newOutputStream(outputChannel));
out.write(bytes);
out.close();
}
.
Grazie in anticipo.
Soluzione
risolto!
Ho avuto 2 problemi.
- .
-
Stavo codificando la stringa in modo improprio.Quando ho effettivamente guardato il JSON outplito, potrei vedere che c'erano "_" nella stringa.Ho risolto questo usando aggiungendo la linea
string s= base64.codetostring (B, base64.default);
-
Non stava avvolgendo l'array Byte in un bytebuffer, prima di caricare su GCS:
GCSoutputChannel EXPUTHCHANNEL= GCSService.CreateRoReplace (nome, optionsbuilder.build ());
outputchannel.write (bytebuffer.wrap (byte)); outputchannel.close ();
L'avevo provato prima, ma l'immagine non avrebbe ancora visuato correttamente.
- .