in Education by
So i want to save the RelativeLayout (360x360dp) into the phone as a picture. Here is the code that i found online, and on stackoverflow but it does not work. Using api 24. package com.femindharamshi.instapostcreator; import android.graphics.Bitmap; import android.graphics.Canvas; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; public class MainActivity extends AppCompatActivity { EditText caption; TextView captionTv; Button savebtn; RelativeLayout image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = findViewById(R.id.image); caption = findViewById(R.id.caption); captionTv = findViewById(R.id.captionTV); savebtn = findViewById(R.id.savebtn); caption.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { captionTv.setText(s); } }); savebtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Called", Toast.LENGTH_SHORT).show(); saveImage(); } }); } public void saveImage() { Bitmap bitmap; bitmap= viewToBitmap(image); saveImageToExternalStorage(bitmap); // try { // FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png"); // bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); // output.close(); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } } private void saveImageToExternalStorage(Bitmap finalBitmap) { String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); File myDir = new File(root + "/saved_images"); myDir.mkdirs(); Random generator = new Random(); int n = 10000; n = generator.nextInt(n); String fname = "Image-" + n + ".jpg"; File file = new File(myDir, fname); if (file.exists()) file.delete(); try { FileOutputStream out = new FileOutputStream(file); finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } // Tell the media scanner about the new file so that it is // immediately available to the user. MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "-> uri=" + uri); } }); } public Bitmap viewToBitmap(View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } } Yes, i have in my androidmanifest. How to save the image ? What is the actual working way ? I want to store in in a the root of the main storage. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Make sure u have this: ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); ActivityCompat.requestPermissions(Pedigree.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

Related questions

0 votes
    I want to store the hex values into a string, but I don't know to do that when my string is not giving me the ... using namespace std; int main(){ string s2 = "HelloWorld"; cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I want to store the hex values into a string, but I don't know to do that when my string is not giving me the ... using namespace std; int main(){ string s2 = "HelloWorld"; cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Write a JAVA program to store the temperature in Celsius and convert it into Fahrenheit using the following formula ... INPUT METHOD Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    Which of the following is used to store movie and image files? (a) Clob (b) Blob (c) Binary (d ... and Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Convert a .doc or .pdf to an image and display a thumbnail in Ruby? Does anyone know how to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Convert a .doc or .pdf to an image and display a thumbnail in Ruby? Does anyone know how to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Convert a .doc or .pdf to an image and display a thumbnail in Ruby? Does anyone know how to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Background: I'm trying to look through assets in an old game's archive files. They're 8-bit. I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    How to remove a image in imageview in android and also how to hide the entire image. Here I have ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    i have this problem that i want to upload image on a server through multi-part form but its keep ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    i have this problem that i want to upload image on a server through multi-part form but its keep ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I'm developing an Android app and I'm working on photo uploading to my rails server. If I upload ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I am completely new with Android/Eclipse. I can't figure out how to add an image in the /res/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I am completely new with Android/Eclipse. I can't figure out how to add an image in the /res/ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I'm trying to overlay 2 ImageViews, one with the background and one with an animated (using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
...