Save pictures into different folder - android

I have this code for take picture from my app, then save into different directory than default:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
directory_path = "/tempCamera";
directory = new File(Environment.getExternalStorageDirectory(),
directory_path);
if (!directory.exists()) {
directory.mkdirs();
}
camera_button = (Button) findViewById(R.id.camera_button);
camera_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent camera_intent = new Intent(
MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(directory));
MainActivity.this.startActivityForResult(camera_intent, 0);
}
});
}
but pictures is always saved into default directory. What's wrong?

You should pass to the MediaStore.EXTRA_OUTPUT, not a path to folder, but a path to a result image.

Related

In correct url to display a pdf on android

I'm trying to display a local pdf file on my android app but it says that the pdf file doesn't exists ,the pdf file it's in the project's assets folder
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programa);
verPdf = (Button)findViewById(R.id.button1);
verPdf.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Uri file= Uri.parse("file:///assets/miarchivo.pdf");
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(file.toString()));
try{
Intent i;
i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(file,mimeType);
startActivity(i);
}catch (ActivityNotFoundException e) {
Toast.makeText(v.getContext(),
"No Application Available to fiew this file type",
Toast.LENGTH_SHORT).show();
}
}
});
}

view pdf in android using program or javascript

I'm new to android development. I have a link in HTML page which is having a PDF document URL as h ref, while clicking the link i need to open the PDF in adobe reader. if any one knows how to do guide me.
Try this :
public class OpenPdf extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
Hope this helps.

Saving bitmap at custom path and with custom name

my program captures an image (as bitmap) but I don't know (and couldn't find) how to save it with a custom name at a custom path... Here's my image capture codes. Can I do it? Or is there any way saving it as another data type, not bitmap? some help will be great. Thanks.
public class ShowMessagesPage extends Activity {
final static int CAMERA_RESULT = 0;
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_messages_page);
Button userButton = (Button)findViewById(R.id.button1);
userButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
error();
}});
Button buttonPhoto = (Button)findViewById(R.id.buttonPhoto);
buttonPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);
}});}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
}
}
and the other codes..........
Try this:
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"folder_name_of_your_choice");
else
cameraFolder= ShowMessagesPage.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
File photo = new File(Environment.getExternalStorageDirectory(), "folder_name_of_your_choice/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, CAMERA_RESULT);
And in the onActivityResult(), process the Uri initialURI to get your Image which is already saved to the path you have chosen in the code posted above. The name of the image file will be: camera_snap.jpg
Note: The Uri initialURI is declared globally.

How can I open a PDF file via my Android app?

I want to open a pdf file via an Android app. So far I have written the following code:
public class PdfopenActivity extends Activity {
String selectedFilePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Sample.pdf is my file name it is in /Root/Download/Sample.pdf
// path of the file
File file = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sample.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
}
The pdf is 175kb and I can open the file directly on my Galaxy Tab2 tablet, but when I run my my program to open it I get the error:
An Error Occurred while opening the document.
Can anyone tell me where I am going wrong?
.
Try this out:
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/sample.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
}
}
}
});
Hope this will help you.

How to open gallery to show images in a specific directory [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Gallery with folder filter
My app creates pictures in a directory in sdcard and I want to open gallery for it. To do that, I simply do following:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivity(intent);
But it shows every pictures in the device. Is it possible to show only files in the directory?
You just need to implement MediaScannerConnectionClient in your activity and after that you have to give the exact path of one of the file inside that folder name here as SCAN_PATH and it will scan all the files containing in that folder and open it inside built in gallery. So just give the name of you folder and you will get all the files inside including video. If you want to open only images change FILE_TYPE="images/*"
public class sdActivity extends Activity implements MediaScannerConnectionClient{
public String[] allFiles;
private String SCAN_PATH ;
private static final String FILE_TYPE = "*/*";
private MediaScannerConnection conn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File folder = new File("/sdcard/youfoldername/");
allFiles = folder.list();
// uriAllFiles= new Uri[allFiles.length];
for(int i=0;i<allFiles.length;i++)
{
Log.d("all file path"+i, allFiles[i]+allFiles.length);
}
// Uri uri= Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0]));
SCAN_PATH=Environment.getExternalStorageDirectory().toString()+"/yourfoldername/"+allFiles[0];
Log.d("SCAN PATH", "Scan Path " + SCAN_PATH);
Button scanBtn = (Button)findViewById(R.id.scanBtn);
scanBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
startScan();
}});
}
private void startScan()
{
Log.d("Connected","success"+conn);
if(conn!=null)
{
conn.disconnect();
}
conn = new MediaScannerConnection(this,this);
conn.connect();
}
#Override
public void onMediaScannerConnected() {
Log.d("onMediaScannerConnected","success"+conn);
conn.scanFile(SCAN_PATH, FILE_TYPE);
}
#Override
public void onScanCompleted(String path, Uri uri) {
try {
Log.d("onScanCompleted",uri + "success"+conn);
if (uri != null)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
} finally
{
conn.disconnect();
conn = null;
}
}
}
i already answered it on this link also Built-in gallery in specific folder.

Categories

Resources