Categories
Android 🏴‍☠️

Get filename and path from uri from mediastore

[sourcecode language=”css”]
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
[/sourcecode]

ref:
http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore

Categories
Android 🏴‍☠️

Camera code2 : push an intent to default camera application to make it take a photo, save it and return an URI

push an intent to default camera application to make it take a photo, save it and return an URI.

TestCamera04Activity.java
[sourcecode language=”css”]
package test.testcamera04;

import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TestCamera04Activity extends Activity {
/** Called when the activity is first created. */
TextView textSavePic ;
private Uri imageUri;

private static final int TAKE_PHOTO_CODE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textSavePic = (TextView)findViewById(R.id.textView1);
}

public void CameraOnclick(View v){

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory()+File.separator+"MyApp",
"PIC"+System.currentTimeMillis()+".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
// startActivityForResult(intent, 0);
startActivityForResult(intent, TAKE_PHOTO_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case TAKE_PHOTO_CODE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
ContentResolver cr = getContentResolver();
Bitmap bitmap;

try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);

textSavePic.setText(selectedImage.toString());
Toast.makeText(this, selectedImage.toString(),Toast.LENGTH_LONG).show();
} catch (Exception e) {
textSavePic.setText(e.toString());
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.e("Camera", e.toString());
}
}
}
}

}
[/sourcecode]

Categories
Android 🏴‍☠️

Camera code01

TestCamera02Activity.java

[sourcecode language=”css”]
package test.testcamera02;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class TestCamera02Activity extends Activity implements SurfaceHolder.Callback{

Button buttonTakePicture;
Button buttonFlash;
Parameters parameters;
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;

String stringPath = "/sdcard/samplevideo.3gp";

final int RESULT_SAVEIMAGE = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initCamera();
initTakePicture();
initFocus();
initFlash();
}
//———————————————————–
private void initCamera(){
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera.stopPreview();
previewing = false;
}

if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
//———————————————————–
private void initTakePicture(){
buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(TakePictureClickListener);
}

private Button.OnClickListener TakePictureClickListener = new Button.OnClickListener(){
// @Override
public void onClick(View v) {
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}
};

private ShutterCallback myShutterCallback = new ShutterCallback(){
//@Override
public void onShutter() {
// TODO Auto-generated method stub

}};

private PictureCallback myPictureCallback_RAW = new PictureCallback(){
//@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub

}};

private PictureCallback myPictureCallback_JPG = new PictureCallback(){
// @Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub

Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS;

try {
imageFileOS = getContentResolver().openOutputStream(uriTarget);
imageFileOS.write(arg0);
imageFileOS.flush();
imageFileOS.close();

Toast.makeText(TestCamera02Activity.this,
"Image saved: " + uriTarget.toString(),
Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
}
};
//———————————————————–
private void initFocus(){

LinearLayout layoutBackground = (LinearLayout)findViewById(R.id.background);
layoutBackground.setOnClickListener(BackgroundClickListener);
}

private Button.OnClickListener BackgroundClickListener = new Button.OnClickListener(){
// @Override
public void onClick(View v) {
buttonTakePicture.setEnabled(false);
camera.autoFocus(myAutoFocusCallback);
}
};

private AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
//@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
// TODO Auto-generated method stub
buttonTakePicture.setEnabled(true);
}
};
//———————————————————–
private void initFlash(){

buttonFlash = (Button)findViewById(R.id.takeflash);
buttonFlash.setOnClickListener(TakeFlashClickListener);
}

private Button.OnClickListener TakeFlashClickListener = new Button.OnClickListener(){
// @Override
public void onClick(View v) {

parameters = camera.getParameters();
if(buttonFlash.getText()=="flash off"){
// parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(parameters);
buttonFlash.setText("flash on");
}else{
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
buttonFlash.setText("flash off");

}

}
};

}[/sourcecode]

Categories
Android 🏴‍☠️

eclipse : autocomplete has stopped working with android sdk

Window -> Preferences -> Java -> Editor -> Content Assist -> Advanced

And check the boxes labeled “Java Proposals” and hit Apply.