2017-05-11 1 views
0

Ich habe eine ImageLoading Klasse erstellt, die Daten in lokalen Speicher herunterlädt und speichert. Ich rufe es auch von mehreren Aktivitäten an. Nach dem Speichern muss ich die Sammlung der Klasse aktualisieren, aus der die Instanz asyncTask erstellt wurde. Ich möchte nicht verwenden, wenn Anweisungen in Aufgabe dies behandeln, und ich möchte diese Aufgabe nicht auf andere Aktivitäten erben. Bitte schlage mir vor, wie es geht. Ich teile meinen Code hier.Ich möchte generische AsyncTask erstellen, die für alle Aktivitäten verwendet werden kann, um zu speichern Image

public class LoadImageTask extends AsyncTask<Void, Void, Bitmap> { 

    private String url; 
    private ImageView imageView; 
    private long ID; 
    private Context context; 
    private SQLiteHandler sqLiteHandler; 
    private String imageType; 
    private int position; 

    public LoadImageTask(String url, ImageView imageView, long ID, Context context, String imageType, int position) { 
     this.url = url; 
     this.imageView = imageView; 
     this.ID = ID; 
     this.context = context; 
     this.imageType = imageType; 
     this.position = position; 
     sqLiteHandler = new SQLiteHandler(context); 
    } 

    String getRandomString(int length) 
    { 
     String data = ""; 
     Random rand = new Random(); 

     for (int i = 1 ; i <= length ; i++) { 
      int n = rand.nextInt(26) + 97; 
      data += String.valueOf((char)n); 
     } 

     return data; 
    } 

    private File getOutputMediaFile(){ 
     File mediaStorageDir = new File(Environment.getExternalStorageDirectory() 
       + "/Android/data/" 
       + context.getPackageName() 
       + "/Files"); 

     if (! mediaStorageDir.exists()){ 
      if (! mediaStorageDir.mkdirs()){ 
       return null; 
      } 
     } 

     String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmssSSS").format(new Date()); 
     timeStamp = String.format("%s_%s", timeStamp, getRandomString(10)); 
     File mediaFile; 
     String mImageName="Image_"+ timeStamp +".jpg"; 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName); 
     return mediaFile; 
    } 

    private void storeImage(Bitmap image) { 
     File pictureFile = getOutputMediaFile(); 
     if (pictureFile == null) { 
      Log.d("Error:","Error creating media file, check storage permissions: "); 
      return; 
     } 
     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      image.compress(Bitmap.CompressFormat.PNG, 90, fos); 
      fos.close(); 
      ImageDownload imageDownload = new ImageDownload(ID,pictureFile.getAbsolutePath()); 
      if(imageType.equals(Constants.ImageType.PROFILE)) { 
       sqLiteHandler.UpdateUserImagePath(imageDownload,Constants.ImageType.PROFILE); 
      } 
      else if(imageType.equals(Constants.ImageType.BANNER)) { 
       sqLiteHandler.UpdateUserImagePath(imageDownload,Constants.ImageType.BANNER); 
      } 
      else if(imageType.equals(Constants.ImageType.SUBJECT)) { 
       if(sqLiteHandler.UpdateUserImagePath(imageDownload,Constants.ImageType.SUBJECT)){ 
        ((MainActivity) context).subjectsItems.get(position).Image = imageDownload.ImagePath; 
        //((MainActivity) context).subjectsItems.get(position).Image = imageDownload.ImagePath; 
        //((MessageList) context).messageDataItems.get(position).SubjectImage = imageDownload.ImagePath; 
       } 
      } 

     } catch (FileNotFoundException e) { 
      Log.d("Error:", "File not found: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.d("Error:", "Error accessing file: " + e.getMessage()); 
     } 
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) { 
     try { 
      URL urlConnection = new URL(url); 
      HttpURLConnection connection = (HttpURLConnection) urlConnection 
        .openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     Drawable d = new BitmapDrawable(imageView.getResources(), result); 
     imageView.setBackground(d); 
     storeImage(result); 
     //imageView.setImageBitmap(result); 
    } 

} 
+0

Können Sie mehr Erklärung für Ihren Code zur Verfügung stellen? Im aktuellen Zustand ist nur ein Code-Dump nicht hilfreich. Was ist der Bereich zum Speichern und Aktualisieren Ihres Codes? – Whitecat

+0

In doBackground lade ich gerade das Image herunter und in storeImage speichere ich Bitmap auf lokalen Speicher. Und dort habe ich Problem der Aktualisierung Feld ((MainActivity) -Kontext) .subjectsItems.get (Position) .Image = imageDownload.ImagePath; Hier habe ich Kontexte von mehreren Klassen und ich muss dort Felder von hier aktualisieren. Ich möchte wenn nicht anders auf andere Kontexte zugreifen. Ich habe auch get von asycTask verwendet und es löste mein Problem.Aber ich möchte diese Art der Umsetzung wissen. Bitte lösen Sie mein Problem. Vielen Dank. –

+0

Die Frage ist nicht klar. Können Sie Ihre Frage so bearbeiten, dass sie klar angibt, welche Frage Sie haben, vorzugsweise als erster Satz? – Whitecat

Antwort

0

einfach tun es einfach so:

((AppCompatActivity) context).subjectsItems.get(position).Image = imageDownload.ImagePath; 
+0

Es funktioniert nicht ... –

+0

Welcher Fehler gibt es ... –

+0

Es ist nicht auf Themenfeld zugreifen. –

Verwandte Themen