What is document missing error in google cloud print for android? I am searching for solutions but not yet found.. Kindly help me..
final Uri docUri = Uri.parse("/mnt/sdcard/downloads");
final String docMimeType = "pdf";
final String docTitle = "Android Interview Questions";
btn_print.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent printIntent = new Intent(PrintDialogActivity.this, PrintActivity.class);
printIntent.setDataAndType(docUri, docMimeType);
printIntent.putExtra("title", docTitle);
startActivity(printIntent);
}
});
As far as I know you have to convert the PDF in to base64 and then send it. The best approach to do this would be to use the built in Base64 class that comes with Android and use the method encodeToString to convert your file in to a base64 string.
How to convert file to bytes
File file = new File(path);
int size = (int) file.length();
byte[] bytes = new byte[size];
BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(file));
buffer.read(bytes, 0, bytes.length);
buffer.close();
Related
I have a question, how can I send my audio file to my server, I had tried to convert to base64 but nothing is working. This is my code
declaration of variables:
private MediaRecorder grabacion;
private String archivoSalida = null;
audio obtained with media record:
archivoSalida = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Myrecord.mp3";
grabacion = new MediaRecorder();
grabacion.setAudioSource(MediaRecorder.AudioSource.MIC);
grabacion.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
grabacion.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
grabacion.setOutputFile(archivoSalida);
Convert to base64,the error comes out here:
private String convertirAudString(MediaRecorder audio){
ByteArrayOutputStream array=new ByteArrayOutputStream();
byte[] audioByte = new byte[(int)audio.length()];//error in this line
String audioString = Base64.encodeToString(audioByte,Base64.DEFAULT);
return audioString;
}
Thanks for your all suggestion.
I solve this, with using
File file = new File(Environment.getExternalStorageDirectory() + "/Miaudio.mp3");
byte[] bytes = new byte[0];
bytes = FileUtils.readFileToByteArray(file);
Then I convert it to base64; it works for me very fast.
I am working on a android project where i am converting the image into BASE 64 format and i am able to get the string, but i am unable to store this huge String what i am getting in a string.
I need to pass the resulting string as a parameter to API. but i am getting only half image like below
Because i am getting half image the data is not getting stored properly in the database
If you copied the string from the Logcat then you got only the first part of the string as logging to logcat logs only the first part of long strings.
Better save the base64 string to file. Open in a text editor and then copy and paste.
It might help you -
public void onImageCompress(Bitmap image) {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1= Base64.encodeToString(ba, Base64.DEFAULT);
generateNoteOnSD("imagebytes.txt", ba1 + "");
}
public void generateNoteOnSD(String sFileName, String sBody) {
try {
File root = new File(getExternalFilesDir(sFileName), "image");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
System.out.println("Saved");
} catch (IOException e) {
e.printStackTrace();
}
}
Call onImageCompress method and pass your bitmap. This will generate a file with a string. Now use this link (as you used in your question) convert base64 to image to see the image.
First of all try to test with small sized images then go for big one.
in my android application i encode a video as base 64 like this.
File file=new File(path);
InputStream is = new FileInputStream(file);
int length = (int)file.length();
byte[] bytes = new byte[length];
int a=is.read(bytes,0,length);
String str = Base64.encodeToString(bytes, 0);
is.close();
//send the string to my server....
PHP
$str=$_POST['str'];
$var=base64_decode($str);
$fp = fopen('2013-02-21_14-52-35_968.mp4', 'w');
fwrite($fp,$var);
fclose($fp);
So when the video file is Written, i cant open it. How i can correctly encode a video and decode it from PHP? or what im missing thanks in advanced.
I solve my problem, the issue was I only encode one part of the file. Here my solution:
$fp=fopen("/address".$filename,'w')
while($row=mysql_fetch_array($getChunks)){
$chuncks=$row['chunkpart'];
$var=base64_decode($chunks);
fwrite($fp,$var)
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to read doc and excel file in android?
I have a .doc file saved on my sdcard. I need to read the content of .doc file and show it in a TextView.
Can anyone please tell me how to do this?
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main);
String extPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.Separator;
InputStream inputStream = assetManager.open(extPath + "file.doc");
String text = loadFile(inputStream);
TextView tv = (TextView)findViewById(R.id.txtv);
tv.setText(text);
}
public String loadFile(InputStream inputStream){
ByteArrayOutputStream b = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int length = 0;
while(){
b.write(bytes, 0, length);
}
return new String(b.toByteArray(), "UTF8");
}
One solution would be using the Apache POI Java library for parsing the .doc file.
To get a File on the SD-card in Android, you could use something like
new File(getExternalFilesDir(null), "word.doc");
try this code
File file=new File("/sdcard/word.doc");
if(file.exists())
{
Uri path=Uri.fromFile(file);
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/doc");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(TestActivity.this, "No software for Doc", Toast.LENGTH_SHORT).show();
}
}
currently I'm using kSoap to publish data to C# web services. Now I've come to the part where I need to upload images from my machine using Base64Binary. I searched everywehre internet but couldn't come up with a proper solution.
there is a solution with external Base64 class example but I'm interested in native solution as there is a API from Android 2.2.
Since I'm a newbie I couldn't do it myself. Basically I have a sd card file path of images, I want to convert them into Base64 format to upload.
Hope someone can help me or direct me to proper documents.
Thanks in advance.
Please try this, use Base64.java from link you have specified.
Bitmap bmImage = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "Your filename");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
String encodedString = Base64.encodeBytes( baos.toByteArray() );
You should change extension of this Bitmap.CompressFormat.JPEG statement according to your file type. You can decode a base 64 string to image using following code
byte[] b;
b = Base64.decode("base 64 string");
final Bitmap unscaledBitmap = BitmapFactory.decodeByteArray(b, 0, b.length );
I figured out the issue
String Image64 = null;
try {
Image64 = Base64.encodeToString((getBytesFromFile(new File(path))), DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
//encording
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}