java.lang.NullPointerException for WebView.getFavicon() saving to ByteArrayOutputStream - android

I was trying to add the favicon of a webpage to a sqlite database, but I am getting a java.lang.NullPointerException when trying to save the favicon to a ByteArrayOutputStream via Bitmap.compress.
There is my code fragment:
Bitmap bitmap = view.getFavicon();
mySQLiteAdapter.openToWrite();
ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, byteArrayBitmapStream);
byte[] bArray = byteArrayBitmapStream.toByteArray();
String[] comments = new String[] {view.getUrl()};
String[] commentss = new String[] {view.getTitle()};
int nextInt = new Random().nextInt(1);
try{
mySQLiteAdapter.insert(bArray, commentss[nextInt], comments[nextInt]);
}catch (Exception e) {
e.printStackTrace();
}
mySQLiteAdapter.close();
Why am I getting this error?

Related

Last byte[] only send to the server by using MultipartEntity in android

I have implemented the multipleimage uploading.If i select three value means,all three images are converted into byte[] perfectly but last value only send into server.How to send all three images.I have tried like this
int i=0;
for ( i = 0; i < ImgData.size(); i++) {
Log.d("ImgData(i)--", String.valueOf(ImgData.get(i)));
mImageIds = new ArrayList<String>();
Bitmap bitmap = decodeFile(ImgData.get(i));
String image = getStringImage(bitmap);
mImageIds.add(image);
Log.d("Image--", image);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray;
byteArray = stream.toByteArray();
Log.d("byteArray11--", String.valueOf(byteArray));
// ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image");
entity.addPart("room_images", new ByteArrayBody(byteArray,
"image/jpeg"));
// Add a bitmap
}

Retrieve data from database in Uri form?

i have an image uri using this uri i insert image in the sqlite DataBase but the problem is how can i retrieve image .
this is code:
if(data!=null){
Uri uri = data.getData();
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();
DataBase dataBase=new DataBase(getBaseContext());
dataBase.insertImage(bArray);
Toast.makeText(getBaseContext(),"single item ",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
i want to retrive image??
android docs :
abstract byte[] getBlob(int columnIndex) Returns the value of the
requested column as a byte array.
Example:
Cursor resultSet = mydatbase.rawQuery("Select * from TutorialsPoint",null);
resultSet.moveToFirst();
byte[] raw_image = resultSet.getBlob(1)

Cursor always giving null in android

i want to check orientation of an image so i found some code but it doesn't work because cursor is always null. My code is
File f = createImageFile(bitmap);
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = context.getContentResolver().query(Uri.parse(f.toString()), orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
private static File createImageFile(Bitmap bitmap) {
//create a file to write bitmap data
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File f = new File(fullPath, "image"+ System.currentTimeMillis()+".jpg");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//Convert bitmap to byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (bitmap != null) {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for PNG*/, bos);
}
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return f;
}
But cur always returns null.I searched for converting file to uri so i found two methods Uri.parse(f.toString() and Uri.fromFile(f) but none has worked.

Saving and retreving photos and videos in Parse (Android)

I was looking at the Parse Android docs and saw that to save photos and videos, you have to initialize a new ParseFile with a name and a byte[] of data and save it.
What's the easiest way to convert an image Uri and video Uri to a byte array?
Here are my attempted solutions:
mPhoto = new ParseFile("img", convertImageToBytes(Uri.parse(mPhotoUri)));
mVideo = new ParseFile ("vid", convertVideoToBytes(Uri.parse(mVideoUri)));
private byte[] convertImageToBytes(Uri uri){
byte[] data = null;
try {
ContentResolver cr = getBaseContext().getContentResolver();
InputStream inputStream = cr.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return data;
}
private byte[] convertVideoToBytes(Uri uri){
byte[] videoBytes = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(getRealPathFromURI(this, uri)));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
videoBytes = baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return videoBytes;
}
private String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Video.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null,
null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
The convertImageToBytes and convertVideoToBytes methods work for now, but I'm just wondering If I'm doing doing this correctly.
From Uri to get byte[] I do the following things,
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = new FileInputStream(new File(yourUri));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
byte[] videoBytes = baos.toByteArray(); //this is the video in bytes.

Sending bitmap image to next activity in android and converting it into string

I m not able to convert the bitmap into string and pass it to the JSON by send the intent.putExtra.....And on receiving it on Create method of next activity....
The Default is giving error and i have jpeg images ...
How to fix this problem....
Intent Intent inn=getIntent()
bitmap = (Bitmap) inn.getParcelableExtra("bmp_img");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
public String imageCompression(String image) {
// to compress image
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
Bitmap b = BitmapFactory.decodeFile(image, bmpFactoryOptions);
Bitmap out = Bitmap.createScaledBitmap(b, 300, 300, false);
String[] bits = image.split("/");
String lastOne = bits[bits.length - 1];
File imageFile = null;
File imagesFolder = new File(Environment.getExternalStorageDirectory(),
"CompressedImages");
if (imagesFolder.isDirectory()) {
imageFile = new File(imagesFolder, lastOne);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(imageFile);
out.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {
e.printStackTrace();
}
} else {
imagesFolder.mkdirs();
imageFile = new File(imagesFolder, lastOne);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(imageFile);
out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
File file = new File(imageFile.getPath());
byte imageData[] = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(imageData, 0, imageData.length);
} catch (IOException e1) {
e1.printStackTrace();
}
imageDataString = Base64.encodeToString(imageData, 0);
return imageDataString;
}
Use this methord and for intent passing pass just as String ......
i assume u are navigate from TO Activity A and B in sort of A->B
in A
Uri imageUri = Uri.parse("android.resource://your.package/drawable/fileName");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(intent)
and in B
Intent intent = this.getIntent();
(Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);

Categories

Resources