Answer the question
In order to leave comments, you need to log in
Android. Full size camera image and file name prompt. How to get the?
The task was to make an application for android that takes a photo, asks for a file name and saves it all to the gallery. In agony gave birth to this code
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ID_READ_WRITE_PERMISSION = 99;
private static final int REQUEST_ID_IMAGE_CAPTURE = 100;
File file = null;
String m_Text = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
Button btnStart;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(oclStart);
}
View.OnClickListener oclStart = new View.OnClickListener() {
@Override
public void onClick(View view) {
Capture();
}
};
void Capture()
{
if (Build.VERSION.SDK_INT>=23){
int readPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int writePermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (writePermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED){
this.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_ID_READ_WRITE_PERMISSION);
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final String filename = "";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
m_Text = input.getText().toString();
String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/CAMERA/"+m_Text+".jpg";
Bitmap bp = (Bitmap) data.getExtras().get("data");
try {
FileOutputStream out = new FileOutputStream(file);
bp.compress(Bitmap.CompressFormat.JPEG,100, out);
} catch (FileNotFoundException ex){
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.show();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question