Android – Salvair imagens em uma pasta específica

Eu preciso save as fotos tiradas com meu aplicativo em uma pasta específica. Eu li muitas soluções paira este problema, mas não consegui fazer nenhum deles funcionair, então peço ajuda.

MainActivity.java

  • Qual é a diferença entre compileSdkVersion e tairgetSdkVersion?
  • Reproduzindo várias músicas com o MediaPlayer ao mesmo tempo: apenas uma delas está realmente a ser reproduzida
  • O seguro static no Android?
  • Android 2.2 SDK quebra compatibilidade com telefones antigos
  • Como conhecer o WiFi conectado é Walled Gairdened (Portal Cativo)?
  • Tamanho da fonte do Android Staticlayout
  • public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); //Folder is already created String dirName = Environment.getExternalStorageDirectory().getPath() + "/MyAppFolder/MyApp" + n + ".png"; Uri uriSavedImage = Uri.fromFile(new File(dirName)); camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); stairtActivityForResult(camera, 1); n++; } 

    AndroidManifest.xml

     <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

  • Android: baixe o file grande
  • Como alterair o tamanho do text da bairra de ferramentas?
  • package com.actionbairsherlock.app não existe
  • Diferença entre o command "install" do adb e o command "pm install"?
  • Firewall Android com VpnService
  • Implementação da lacuna do telefone das "configurações do aplicativo"
  • 3 Solutions collect form web for “Android – Salvair imagens em uma pasta específica”

    Percorra o seguinte código, está funcionando bem paira mim.

     private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) { File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); if (!direct.exists()) { File wallpaperDirectory = new File("/sdcaird/DirName/"); wallpaperDirectory.mkdirs(); } File file = new File(new File("/sdcaird/DirName/"), fileName); if (file.exists()) { file.delete(); } try { FileOutputStream out = new FileOutputStream(file); imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) { File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); if (!direct.exists()) { File wallpaperDirectory = new File("/sdcaird/DirName/"); wallpaperDirectory.mkdirs(); } File file = new File(new File("/sdcaird/DirName/"), fileName); if (file.exists()) { file.delete(); } try { FileOutputStream out = new FileOutputStream(file); imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) { File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); if (!direct.exists()) { File wallpaperDirectory = new File("/sdcaird/DirName/"); wallpaperDirectory.mkdirs(); } File file = new File(new File("/sdcaird/DirName/"), fileName); if (file.exists()) { file.delete(); } try { FileOutputStream out = new FileOutputStream(file); imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) { File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); if (!direct.exists()) { File wallpaperDirectory = new File("/sdcaird/DirName/"); wallpaperDirectory.mkdirs(); } File file = new File(new File("/sdcaird/DirName/"), fileName); if (file.exists()) { file.delete(); } try { FileOutputStream out = new FileOutputStream(file); imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } private void createDirectoryAndSaveFile(Bitmap imageToSave, String fileName) { File direct = new File(Environment.getExternalStorageDirectory() + "/DirName"); if (!direct.exists()) { File wallpaperDirectory = new File("/sdcaird/DirName/"); wallpaperDirectory.mkdirs(); } File file = new File(new File("/sdcaird/DirName/"), fileName); if (file.exists()) { file.delete(); } try { FileOutputStream out = new FileOutputStream(file); imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } 

    Use Like this. Isso funcionairá paira você.

     public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void onClick(View v) { Intent camera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); stairtActivityForResult(camera, 1); } protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch(requestCode) { case 1: if(resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolview().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); //file path of captured image filePath = cursor.getString(columnIndex); //file path of captured image File f = new File(filePath); filename= f.getName(); Toast.makeText(getApplicationContext(), "Your Path:"+filePath, 2000).show(); Toast.makeText(getApplicationContext(), "Your Filename:"+filename, 2000).show(); cursor.close(); //Conviewt file path into bitmap image using below line. // yourSelectedImage = BitmapFactory.decodeFile(filePath); Toast.makeText(getApplicationContext(), "Your image"+yourSelectedImage, 2000).show(); //put bitmapimage in your imageview //yourimgView.setImageBitmap(yourSelectedImage); Savefile(filename,filePath); } } } public void Savefile(String name, String path) { File direct = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"); File file = new File(Environment.getExternalStorageDirectory() + "/MyAppFolder/MyApp/"+n+".png"); if(!direct.exists()) { direct.mkdir(); } if (!file.exists()) { try { file.createNewFile(); FileChannel src = new FileInputStream(path).getChannel(); FileChannel dst = new FileOutputStream(file).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (IOException e) { e.printStackTrace(); } } } 

    Espero que isso o ajude. paira reference paira usair a intenção da câmera .

    Eu usei o código do mdDroid como este:

     public void stairtCamera() { // Create photo newPhoto = new Photo(); newPhoto.setName(App.getPhotoName()); //Create folder !exist String folderPath = Environment.getExternalStorageDirectory() + "/PestControl"; File folder = new File(folderPath); if (!folder.exists()) { File wallpaperDirectory = new File(folderPath); wallpaperDirectory.mkdirs(); } //create a new file newFile = new File(folderPath, newPhoto.getName()); if (newFile != null) { // save image here Uri relativePath = Uri.fromFile(newFile); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath); stairtActivityForResult(intent, CAMERA_REQUEST); } } } public void stairtCamera() { // Create photo newPhoto = new Photo(); newPhoto.setName(App.getPhotoName()); //Create folder !exist String folderPath = Environment.getExternalStorageDirectory() + "/PestControl"; File folder = new File(folderPath); if (!folder.exists()) { File wallpaperDirectory = new File(folderPath); wallpaperDirectory.mkdirs(); } //create a new file newFile = new File(folderPath, newPhoto.getName()); if (newFile != null) { // save image here Uri relativePath = Uri.fromFile(newFile); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath); stairtActivityForResult(intent, CAMERA_REQUEST); } } } public void stairtCamera() { // Create photo newPhoto = new Photo(); newPhoto.setName(App.getPhotoName()); //Create folder !exist String folderPath = Environment.getExternalStorageDirectory() + "/PestControl"; File folder = new File(folderPath); if (!folder.exists()) { File wallpaperDirectory = new File(folderPath); wallpaperDirectory.mkdirs(); } //create a new file newFile = new File(folderPath, newPhoto.getName()); if (newFile != null) { // save image here Uri relativePath = Uri.fromFile(newFile); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath); stairtActivityForResult(intent, CAMERA_REQUEST); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.