I'm trying to send PNG file from my android server to my python client.
The PNG image I try to send is a screenshot, around 4mb tops, usually under 2mb.
android code (sending):
File myFile = new File(imagePath);
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
Log.i("service", "sending file len");
try {
out.write("" +myFile.length());
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("service:", "waiteing for ok");
try {
msg = in.readLine();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("service", "sending file");
byte[] outBuffer = new byte[(int) myFile.length()];
try {
bis.read(outBuffer, 0, outBuffer.length);
os = client.getOutputStream();
os.write(outBuffer, 0, outBuffer.length);
} catch (IOException e) {
e.printStackTrace();
}
python code (receiving):
print "waiting for responce's length"
MSGLEN = int(sock.recv(bufferLen))
print MSGLEN
sock.sendall("ok" +"\n")
chunks = []
bytes_recd = 0
while bytes_recd < MSGLEN:
chunk = sock.recv(min(MSGLEN - bytes_recd, bufferLen))
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
dataRecived = ''.join(chunks)
print 'data receieved'
print 'writing data to file'
fileout = open("D:\shots.png", 'w')
fileout.write(dataRecived)
fileout.close()
The file transfers from the android to my PC, but the file is corrupted.
When I compare it with the original image, almost everything is identical
except some empty lines here and there (not missing information, just empty line like someone added \n) and 1 or 2 big chunks of lines (15 lines or so) are missing.
Here you can see comparison between the tho files (left-original, right-file after sending).
I don't know why the file transfers corrupted, please help me.
Try coding it as Base64 and sending a simple string. Those missing lines are also part of image data - remember that those are binary.
Base64 or Bytestream is what u need
Related
SOLVED: SEE BELOW
I'm trying to save some data to a file, so later I can access them.
However, when I write the string, and read it, then it looks good. But when I try to read from the file and display it on a Toast it doesn't show good.
The text is full of spaces.
Here is my code:
try {
fos = openFileOutput("1"+i, Context.MODE_PRIVATE);
DataOutputStream dos = new DataOutputStream(fos);
text = "{id"+ "1"+ i + "id}"+
"{amount" + Amount + "amount}"+
"{category"+ category + "category}"+
"{date"+ date.getText() + "date}"+
"{description"+ description.getText() + "description}";
dos.writeChars(text);
dos.close();
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
The second activity, when it opens fires this code:
try {
FileInputStream fis;
fis = openFileInput("11");
DataInputStream dis = new DataInputStream(fis);
Toast.makeText(getApplicationContext(),
dis.readUTF(), Toast.LENGTH_SHORT).show();
dis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
When the toast shows, the text looks something like this:
{ i d 1 1 i d } ...
EDIT: SOLVED
Instead of using
dos.writeChars(text);
I use: dos.writeUTF(text);
When you read in UTF you should make sure to also write in UTF
I'm having issues using the Cards from the recently released GDK. Basically, Card.addImage() can only take two arguments, a resource id or a URI.
For my use case, I need to open an image that exists as a file not directly as a resource. So for testing purposes I'm including the images in the assets folder. Trying to access them directly from the asset folder fails, so I'm copying them from there to internal storage. Once they're copied, I generate a URI from the file and assign it to the card. The resulting card shows a grey block where the image should be.
String fileName = step.attachment; //of the form, "folder1/images/image1.jpg"
File outFile = new File(getFilesDir()+File.separator+fileName);
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
//check to see if the file has already been cached in internal storage before copying
if(!outFile.exists()) {
FileInputStream inputStream = new FileInputStream(getAssets().openFd(fileName).getFileDescriptor());
FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
inputChannel = inputStream.getChannel();
outputChannel = outputStream.getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(inputChannel!=null)
inputChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(outputChannel!=null)
outputChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
card.addImage(Uri.fromFile(outFile));
It's hard to diagnose because I have no clue what the Card is doing internally.
Instead of writing
new FileInputStream(getAssets().openFd(fileName).getFileDescriptor());
can you try
getAssets().openFd(fileName).createInputStream();
and see if it works?
To answer your original question, the addImage method supports resource: and file: URIs.
This is very strange, but I managed to solve my problem. I replaced the file copy code with the following and it appears to have solved my issues
InputStream in = null;
OutputStream out = null;
try {
in = getAssets().open(step.attachment);
out = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + step.attachment, e);
}
It's not clear to me why/how I was copying my entire apk, but I'm guessing it's the call to
getAssets().openFd(fileName).getFileDescriptor()
Perhaps it was returning the file descriptor of the apk. It's odd because I've seen some claim that the previous method works.
I'm trying to create an image file on sd-card, building it from the bytes that a server is sending towards me after calling a web-service (basically: download file).
I managed to get "something" on the client-side, and try to write those bytes to a file, using:
FileOutputStream fOut = null;
BufferedOutputStream bOs = null;
try {
fOut = new FileOutputStream(returnedFile);
bOs = new BufferedOutputStream(fOut);
bOs.write(bytesToWrite);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (bOs != null) {
bOs.close();
fOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
but the image file is broken (its size is > 0kb, but broken).
I ended up opening that file on my computer with a text editor, and I saw that some of the initial file data (before being sent), differs from the final one. So I'm guessing that there is some kind of encoding misstakening or something like that.
I would appreciate an idea of how to make this work ( download image file from a web server, and open in on my phone).
PS. I can also change or get info about the server configuration, as it was configured by a friend of mine.
PS2. I should be able not to download only images, but any kind of file.
I think It's better you encode the image to Base64 in your server, for example in PHP you can do it like this :
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
And then in android you decode the Base64 string into your image file:
FileOutputStream fos = null;
try {
if (base64ImageData != null) {
fos = context.openFileOutput("imageName.png", Context.MODE_PRIVATE);
byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT);
fos.write(decodedString);
fos.flush();
fos.close();
}
} catch (Exception e) {
} finally {
if (fos != null) {
fos = null;
}
}
First of all make sure you have this permissions on your android manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
File stream is designed to work on local file storage rather than network connection. Use URLConnection class instead.
URL uri = new URL("Your Image URL");
URLConnection connection = uri.openConnection();
InputStream stream = connection.getInputStream();
//DO other stuff....
I need to download by program an .apk file then launching its activity.
The code I am using is the following
private String downloadFile(String sourceURL, String destinationPath)
{
try {
URL url = new URL(sourceURL);
URLConnection connection = url.openConnection();
connection.connect();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(destinationPath);
byte data[] = new byte[1024];
int count;
while ((count = input.read(data)) > 0) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
// EDITED here: Make the file rw, otherwise the apk file will not be installed
Runtime.getRuntime().exec("chmod 666 " + destinationPath);
message = "OK";
}
catch (MalformedURLException e) {
message = "Malformed URL error: " + e.getMessage();
}
catch (ProtocolException e) {
message = "Protocol exception error: " + e.getMessage();
}
catch (FileNotFoundException e) {
message = "File not found error: " + e.getMessage();
}
catch (IOException e) {
e.printStackTrace();
message = "Failed to download the file : " + e.getMessage();
}
return message;
}
I mention that I call the method first for a text file, then for an apk file. Each time I process the files locally, therefore, somehow I know what's going wrong or not. In this way I know that the text file is downloaded correctly. But the .apk file is corrupted. Because I develop locally, with access to DDMS and localhost (the IP: 10.0.2.2) I can firmly state that the culprit is the code above. When I artificially replace the 'downloaded' file, through DDMS, with the original .apk file, all processing that follows is Ok. In addition, I have byes difference when I compare the original and the downloaded .apk files.
What am I doing wrong?
Thanks
PS: Searching, I realized that, while is a popular issue, there is no consistent answer to it. In my case I identified it as purely a download method issue.
Please see above the answer under the EDITED line. Implemented, works fine, that may help others
So in this app I made, The user makes a project and when they save, the number of frames is saved to numberFrames.txt on the SD card. Then I retrieve the file in another class. Only thing is that nFrames = 50 when i show a toast of nFrames to the screen after I run this code. The only initializing of nFrames I do is to zero right above this code, which is located in the onCreate().
File sdcardLocal = Environment.getExternalStorageDirectory();
File dir = new File (sdcardLocal.getAbsolutePath() + "/Flipbook/"+customizeDialog.getTitle()+"/");
dir.mkdirs();
File fileNum = new File(dir, "numberFrames.txt");
FileWriter myFileWriter = null;
try {
myFileWriter = new FileWriter(fileNum);
} catch (IOException e) {
e.printStackTrace();
}
BufferedWriter out = new BufferedWriter(myFileWriter);
try {
String text = bitmaps.size()+"";
out.write(text);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
I retrieve the file like this. I have no idea where this "50" value for nFrames came from as there are no loops around this and I know for sure that the particular project saved has only 3 frames. Why is this?
FileInputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File(mFolderDialog.getPath()+"/numberFrames.txt"));
bis = new BufferedInputStream(is);
nFrames = bis.read();
}catch (IOException e) {
e.printStackTrace();
}
You are writing out a string, and then reading the first byte as an integer. 50 is the ascii code for the '2' character.
You can use BufferedReader.readLine to read the entire first line of the file as a String, and then Integer.parseInt to convert that to an integer.
Also, I would take a closer look at your application's workflow. You don't give much information, but saving a file with a single integer value to the sdcard has a certain "smell" to it :). Have you looked at using a database, or maybe store the text file in your application's directory instead?